do…while loop

  • Last Updated: October 16, 2023
  • By: javahandson
  • Series
img

do…while loop

In this article, we will learn what a do…while loop is and what is a while loop in Java with proper examples. If you want to learn about other Java topics you can click the above Java menu or you can click on this link Java topics.

 

do…while loop

We will use this loop when we want to execute a group of statements repeatedly as long as the condition is true. If the condition is false then the repetition will be stopped and the group of statements will not be executed anymore.

Syntax

do {
  // execute a set of statements
}
while(condition)

If there is only one statement inside the do…while loop then we do not have to use the curly braces.

Write a program to print the numbers from 1 to 5 ( With multiple statements using curly braces )

package com.java.handson.control.statements;

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

        int counter = 1;

        do {
            System.out.println(counter);
            counter++;
        }
        while (counter <= 5);
    }
}
Output : 
1
2
3
4
5

Explanation of the above program

a. We have set the counter = 1.

b. We enter the do loop and print the value of the counter i.e. 1 ( We are not checking the condition at the very first time )

c. We increment the counter by 1. Hence counter = 2.

d. We check the while condition i.e counter <= 5 since 2 <= 5 the condition is true hence we execute the loop again

e. We print the the value of counter i.e. 2 and increment the counter and check the while condition again.

f. This will be repeated till counter = 5. As soon as the counter reaches 6 the while condition i.e. 6 <= 5 becomes false and we come out of the loop.

Write a program to print the numbers from 1 to 5 ( Single statement – No curly braces )

package com.java.handson.control.statements;

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

        int counter = 1;

        do
          System.out.println(counter++);
        while (counter <= 5);
    }
}
Output : 
1
2
3
4
5

Note* In the do…while loop the set of statements will be executed at least once before the condition is validated.

While loop

The While loop is similar to do…while loop. In the while loop, the set of statements gets executed as long as the condition is true. Once the condition is false the loop will be terminated and the statements will not be executed anymore.

Syntax

while {
  // execute a set of statements
}

If there is only one statement inside the while loop then we do not have to use the curly braces.

Write a program to print the numbers from 1 to 5 ( With multiple statements using curly braces )

package com.java.handson.control.statements;

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

        int counter = 1;

        while (counter <= 5)
        {
            System.out.println(counter);
            counter++;
        }
    }
}

Output : 
1
2
3
4
5

Explanation of the above program

a. We have set the counter = 1.

b. We check the while condition i.e. counter <= 5 since 1 <= 5 hence we will enter the loop.

c. We will print the counter i.e. 1 and increment the value of the counter by 1.
Hence counter = 2.

d. We check the while condition i.e. counter <= 5 since 2 <= 5 the condition is true hence we execute the loop again.

e. We print the the value of counter i.e. 2 and increment the counter and check the while condition again.

f. This will be repeated till counter = 5. As soon as the counter reaches 6 the while condition i.e. 6 <= 5 becomes false and we come out of the loop.

Write a program to print the numbers from 1 to 5 ( Single statement – No curly braces )

package com.java.handson.control.statements;

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

        int counter = 1;

        while (counter <= 5)
            System.out.println(counter++);
    }
}

Output : 
1
2
3
4
5

Difference between do…while and while loop

1. In a do…while loop the statements are executed first and then the condition is tested whereas in a while loop, the condition is tested first then the statements are executed.

2. In a do…while loop developer will not have control over the execution of the statements from the very start whereas a while loop will give better control right from the beginning. This is the reason while loop is treated as more efficient than do…while loop.

3. We should use the do…while loop when we are 100 percent sure that we have to execute the set of statements at least once without any condition check.

So this is all about do…while statement in Java. I hope you like the article. If you have any questions on this topic please raise them in the comments section.

Leave a Comment