Java-While Loop

While Loop

In many programming situations, there arises a need for certain instructions to be executed over and over again. A special type of statements called the looping statements shall be used in such situations. The while statement, the for statement and the do…while statement are the looping statements available in Java. The While loop is usually used when the number of iterations is predetermined. The general format of while loop is :

The ‘test condition’ is evaluated and if the condition is true, then the ‘body of the loop’ is executed. After execution of the body, the test condition is ones again evaluated and if it is true, the body is executed once again. This process of repeated execution of the body continues until the test condition finally becomes false and the loop is terminated.

Program

1

Program Source

class Javaapp {

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

Leave a Comment