Java: While and Do While Loop

Next, let us look at the while loop. The term is rather self-explanatory as it implies that the statements within a while loop are executed as long as the particular condition mentioned in the while loop remains true. A while loop looks like this: 

while (condition is true) {
    do A

Typically, while executing a while loop, a variable must be assigned to act as a loop counter. For the sake of convenience, this variable is called the counter. The code stated below is a simple example wherein the functionality of the while loop becomes apparent.

public class WhileJava { 
    public static void main(String[] args) { 

        int counter = 6;
        while (counter > 0) {
            System.out.println("Counter = " + counter); 
            counter = counter - 1;

Running this code will display the following output:

Counter = 6
Counter = 5
Counter = 4
Counter = 3
Counter = 2
Counter = 1

The syntax of the while loop is not as complex. Until the counter remains greater than 0, the statements enclosed within the curly brackets will be executed.  

The statement counter = counter – 1 is very important for our code as the counter is reduced by 1 every time the loop is executed.  

The purpose of decreasing the counter by 1 is so that at some point, the loop condition (counter > 0) returns false. The failure to integrate that statement will set our loop to infinite runs. The program will continue printing the value of the counter as 5 until the program is terminated. It is especially challenging if the code is pretty long and coders cannot spot the part causing the infinite loop.

Do-while Loop

The primary difference between a do-while and a while loop is that the ‘do’ statements are executed at least once in the former.  Let us look at an example of a do-while to understand better how it works.

public class DoWhileJava { 
    public static void main(String[] args) {

        int counter = 20;
        do {
            System.out.println("Counter inside the loop = " + counter); 
        } while (counter < 0);
        System.out.println("Counter outside the loop = " + counter);

According to the test condition, the program is expected to run only if the counter’s value is less than zero. However, the condition is positioned after the do statements that are enclosed within the curly brackets. Thus, it is only assessed after printing the do statement for the first time. 

The code above will return the counter value as

Counter inside the loop = 20
Counter outside the loop = 21

Upon the execution of the System.out.println(“Counter = ” + counter); statement, an increase in the counter value by 1 is executed. The counter now becomes 21. The test condition is returned false after that since 21 is not less than zero. The loop is then exited. Although the counter’s initial value does not agree with the test condition, the code enclosed within the curly brackets is executed the first time.  

Remember that a semi-colon (;) is mandatory after the test condition.  

Leave a Reply

%d bloggers like this: