DEV Community

SATHISH BALAJI
SATHISH BALAJI

Posted on

While Loop

Java while loop

The while loop is also used to iterate over the number of statements multiple times. However, if we don't know the number of iterations in advance, it is recommended to use a while loop. Unlike for loop, the initialization and increment/decrement doesn't take place inside the loop statement in while loop.

It is also known as the entry-controlled loop since the condition is checked at the start of the loop. If the condition is true, then the loop body will be executed; otherwise, the statements after the loop will be executed.

Image description

Calculation .java

    public class Calculation {    
    public static void main(String[] args) {    
    // TODO Auto-generated method stub    
    int i = 0;    
    System.out.println("Printing the list of first 10 even numbers \n");    
    while(i<=10) {    
    System.out.println(i);    
    i = i + 2;    
    }    
    }    
    }    
Enter fullscreen mode Exit fullscreen mode

Output:

Printing the list of first 10 even numbers 

0
2
4
6
8
10
Enter fullscreen mode Exit fullscreen mode

Top comments (0)