Home >>Java Tutorial >Java While Loop

Java While Loop

Java While Loop

The while loop in Java, is generally used to iterate a part of the specific program multiple times. The while loop in Java, is generally recommended to be use used whenever there is a condition that the number of iteration are not certain.

Syntax

Here is the syntax of the While loop in Java depicted below:

while(condition){  
//code that is to be executed  
}  

Here is an example that will explain the use of the while loop in Java to enhance your grasping process of the concept:

public class while {  
public static void main(String[] args) {  
    int i=1;  
    while(i<=5){  
        System.out.println(i);  
    i++;  
    }  
}  
}  
Output:
1
2
3
4
5

Java Infinitive While Loop

Whenever there is a condition in which the programmer passes true in the while loop in Java language then this will become an infinitive while loop.

Syntax

Here is the syntax of the infinitive while loop as depicted below:

while (true) {  
//code that is to be executed  
} 

Here is an example that will explain the use of the infinitive while loop in Java language that will make you understand the application aspect of this topic:

public class while {  
public static void main(String[] args) {  
    while(true){  
        System.out.println("infinitive while loop");  
    }  
}  
}  
Output:
infinitive while loop
infinitive while loop
infinitive while loop
infinitive while loop
infinitive while loop
.
.
.
.
.
.
.

No Sidebar ads