Java while loop is the simplest type of loops. It executes the statements repeatedly. While loop is an entry control loop means the condition is checked at the beginning of the loop.

Java While Loop Syntax:

while(condition)
{
//statement will be here
}

Java While Loop Example:

public class DemoWhile {  
public static void main(String[] args) {  
    int n=1;  
    while(n<=5)
 {  
        System.out.println("number is "+n);  
 n++;  
 }  
}  
}

It will generate the following output-

Number is 1 
Number is 2 
Number is 3 
Number is 4 
Number is 5

Infinite While Loop

If the test condition of the while loop will be always true then it is an infinite while loop. Syntax:

while(true)
{
//statement will be here
}

Let’s see the following example of the infinite while loop-

public class DemoWhile {  
public static void main(String[] args) {  
    int n=1;  
    while(n==1)
 {  
        System.out.println("number is "+n);  
 n++;  
 }  
}  
}