While Loop Java Tutorial

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

Syntax:

while(condition){ 
        //Block of statements
}

Program to use while loop example in java.

/**
 * Program to use while loop example in java.
 * @author javawithease
 */
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

Download this example.