0% found this document useful (0 votes)
97 views2 pages

Java While Loop Basics

The Java while loop iterates code a variable number of times as long as a condition is true. It checks the condition before running the code block. An infinite loop results if the condition is always true. A while loop example prints numbers 1 to 10, incrementing the counter i each iteration. Passing true in the condition creates an infinite loop that continuously prints a message until ctrl+c is pressed to terminate the program.

Uploaded by

Venu D
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
97 views2 pages

Java While Loop Basics

The Java while loop iterates code a variable number of times as long as a condition is true. It checks the condition before running the code block. An infinite loop results if the condition is always true. A while loop example prints numbers 1 to 10, incrementing the counter i each iteration. Passing true in the condition creates an infinite loop that continuously prints a message until ctrl+c is pressed to terminate the program.

Uploaded by

Venu D
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

Java While Loop

The Java while loop is used to iterate a part of the program several times. If the number of
iteration is not fixed, it is recommended to use while loop.

Syntax:

1. while(condition){  
2. //code to be executed  
3. }  

Example:

1. public class WhileExample {  
2. public static void main(String[] args) {  
3.     int i=1;  
4.     while(i<=10){  
5.         System.out.println(i);  
6.     i++;  
7.     }  
8. }  
9. }  
Test it Now
Output:

1
2
3
4
5
6
7
8
9
10

Java Infinitive While Loop


If you pass true in the while loop, it will be infinitive while loop.

Syntax:

1. while(true){  
2. //code to be executed  
3. }  

Example:

1. public class WhileExample2 {  
2. public static void main(String[] args) {  
3.     while(true){  
4.         System.out.println("infinitive while loop");  
5.     }  
6. }  
7. }  

Output:

infinitive while loop


infinitive while loop
infinitive while loop
infinitive while loop
infinitive while loop
ctrl+c

Now, you need to press ctrl+c to exit from the program.

You might also like