While Loop Example Java Program


Definition

A while loop is a control flow statement that allows code to be executed repeatedly based on a given boolean condition. The while loop can be thought of as a repeating if statement. The while construct consists of a block of code and a condition/expression.

Syntax

while(Condition){
	//DO something
}

While Loop Example Program

class WhileLoopExample{
	public static void main(String[] args){
		int num=0;
		while(num<=5){
			System.out.println(""+num);
			num++;
		}
	}
}

Sample Output

Output is:
0
1
2
3
4
5