The while loop is one of the fundamental loop statements in Java. It is an entry-control loop that is it checks the condition at the beginning of the block. And repeats a statement or block until the condition is true. And the increment of the variable checked in condition checking is done inside the block as shown in the below example.
Flowchart for While loop in Java:
Syntax of while loop:
1 2 3 4 | while(condition) { //block of code to be executed } |
Example of while loop in Java:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | public class WhileTest { public static void main(String args[]) { int a = 1; while( a < 10 ) { System.out.println("value of a : " + a ); a++; //increment } } } |
Output of while Loop:
1 2 3 4 5 6 7 8 9 | value of a : 1 value of a : 2 value of a : 3 value of a : 4 value of a : 5 value of a : 6 value of a : 7 value of a : 8 value of a : 9 |