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:

Example:
- public class WhileExample {
- public static void main(String[] args) {
- int i=1;
- while(i<=10){
- System.out.println(i);
- i++;
- }
- }
- }
Output:
Java Infinitive While Loop
If you pass true in the while loop, it will be infinitive while loop.
Syntax:
Example:
- public class WhileExample2 {
- 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
ctrl+c
Now, you need to press ctrl+c to exit from the program.
A while loop statement in java programming language repeatedly executes a target statement as long as a given condition is true.
Syntax:
The syntax of a while loop is:
while(Boolean_expression)
{
//Statements
}
Here, statement(s) may be a single statement or a block of statements. Thecondition may be any expression, and true is any non zero value.
When executing, if the boolean_expression result is true, then the actions inside the loop will be executed. This will continue as long as the expression result is true.
When the condition becomes false, program control passes to the line immediately following the loop
Flow Diagram

Here, key point of the while loop is that the loop might not ever run. When the expression is tested and the result is false, the loop body will be skipped and the first statement after the while loop will be executed.