In the last tutorial, we learnt for loop and enhanced for loop  and in this tutorial we will discuss while loop.

The while statement continually executes a block of statements while a particular condition is true.

Syntax:

1
2
3
while (expression) {
     // statement(s)
}

If the expression of while statement evaluates to true, then it executes the statement(s) in the while block. The while statement continues testing the expression and executing its block until the expression evaluates to false.

Sample Program:

1
2
3
4
5
6
7
8
9
10
11
12
13
package ClassThreeControlFlowStatements;
 
public class WhileLoop {
 
public static void main(String[] args) {
            int i = 1;
            while (i<=10){
                 System.out.println(“Value of i is “+i);
                 i++;
            }
        }
 
}

Must Read: Java Tutorial