Java While Loop

The while loop repeatedly executes a block of statements until a particular condition is true. It first checks the condition and executes a block of statements if the condition is true.

 

Syntax

while(condition){ 
        //Block of statements
}

 

Program to use while loop example in Java.

public class WhileLoopExample {	
	static void whileLoopTest(int num){
		int i = 1;
		//while loop test
		 while(i<=num){
			 System.out.println(i);
			 i++;			 
		 }
	}	
	
	public static void main(String args[]){ 
		//method call 
		whileLoopTest(10); 
	}
}

Output

1
2
3
4
5
6
7
8
9
10