Facebook Pixel

while Loop

Java While Loop

Whenever we are not sure about the number of times the loop needs to be run, we use a while loop. A while loop keeps on running till the condition is true; as soon as the condition is false, control is returned to the main body of the program.

Syntax:

while (baseBooleanCondition) {
    // block of code
}

baseBooleanCondition: It checks if the condition is true or false after each iteration. If true, run the block of code. If false, terminate the loop.

Example:

public class WhileLoop {
    public static void main(String[] args) {
        int i = 10;
        while (i > 0) {
            System.out.println(i);
            i--;
        }
    }
}

Output:

10
9
8
7
6
5
4
3
2
1
while Loop | Java Tutorial | CodeWithHarry