Java do-while loop with Examples - GeeksforGeeks

01 Apr, 2022
Loops in Java come into use when we need to repeatedly execute a block of statements. Java do-while loop is an Exit control loop. Therefore, unlike for or while loop, a do-while check for the condition after executing the statements of the loop body.
Syntax: 
do
{
    // Loop Body
    Update_expression
}

// Condition check
while (test_expression);
Illustration:
Java
// Java Program to Illusterate One Time Iteration// Inside do-while Loop// When Condition IS Not Satisfied // Classclass GFG {     // Main driver method    public static void main(String[] args)    {        // initial counter variable        int i = 0;         do {             // Body of loop that will execute minimum            // 1 time for sure no matter what            System.out.println("Print statement");            i++;        }         // Checking condition        // Note: It is being checked after        // minimum 1 iteration        while (i < 0);    }}
Print statement
Output explanation:
In the above code, we figured out that the condition is checked later as the body inside do will get executed one time without fail as the condition is checked later onwards. Hence whenever we want to display the menu and later on proceed command on the terminal, we always use do-while loop.
A. Test Expression: In this expression, we have to test the condition. If the condition evaluates to true then we will execute the body of the loop and go to update expression. Otherwise, we will exit from the while loop. For example: 
i <= 10
B. Update Expression: After executing the loop body, this expression increments/decrements the loop variable by some value. For example:  
i++;
Control falls into the do-while loop.The statements inside the body of the loop get executed.Updation takes place.The flow jumps to ConditionCondition is tested. If Condition yields true, go to Step 6.If Condition yields false, the flow goes outside the loopThe flow goes back to Step 2.
Control falls into the do-while loop.
The statements inside the body of the loop get executed.
Updation takes place.
The flow jumps to Condition
Condition is tested. If Condition yields true, go to Step 6.If Condition yields false, the flow goes outside the loop
If Condition yields true, go to Step 6.If Condition yields false, the flow goes outside the loop
If Condition yields true, go to Step 6.
If Condition yields false, the flow goes outside the loop
The flow goes back to Step 2.
Implementation:
Example 1: This program will try to print “Hello World” 5 times.  
Java
// Java Program to Illustrate Do-while Loop // Classclass GFG {     // Main driver method    public static void main(String args[])    {         // Declaring and initialization expression        int i = 1;         // Do-while loop        do {             // Body od do-while loop            // Print statement            System.out.println("Hello World");             // Update expression            i++;        }         // Test expression        while (i < 6);    }}
Hello World
Hello World
Hello World
Hello World
Hello World
 
Auxiliary Space: O(1)
Output explanation: 
The program will execute in the following manner as follows:
Program starts.i is initialized with value 1.Execution enters the loop“Hello World” gets printed 1st time.Updation is done. Now i = 2.Condition is checked. 2 < 6 yields true.Execution enters the loop.“Hello World” gets printed 2nd time.Updation is done. Now i = 3.Condition is checked. 3 < 6 yields true.Execution enters the loop“Hello World” gets printed 3rd timeUpdation is done. Now i = 4.Condition is checked. 4 < 6 yields true.Execution enters the loop“Hello World” gets printed 4th timeUpdation is done. Now i = 5.Condition is checked. 5 < 6 yields true.Execution enters the loop“Hello World” gets printed 5th timeUpdation is done. Now i = 6.Condition is checked. 6 < 6 yields false.The flow goes outside the loop.
Program starts.
i is initialized with value 1.
Execution enters the loop“Hello World” gets printed 1st time.Updation is done. Now i = 2.
“Hello World” gets printed 1st time.
Updation is done. Now i = 2.
Condition is checked. 2 < 6 yields true.
Execution enters the loop.“Hello World” gets printed 2nd time.Updation is done. Now i = 3.
“Hello World” gets printed 2nd time.
Updation is done. Now i = 3.
Condition is checked. 3 < 6 yields true.
Execution enters the loop“Hello World” gets printed 3rd timeUpdation is done. Now i = 4.
“Hello World” gets printed 3rd time
Updation is done. Now i = 4.
Condition is checked. 4 < 6 yields true.
Execution enters the loop“Hello World” gets printed 4th timeUpdation is done. Now i = 5.
“Hello World” gets printed 4th time
Updation is done. Now i = 5.
Condition is checked. 5 < 6 yields true.
Execution enters the loop“Hello World” gets printed 5th timeUpdation is done. Now i = 6.
“Hello World” gets printed 5th time
Updation is done. Now i = 6.
Condition is checked. 6 < 6 yields false.
The flow goes outside the loop.
Example 2
Java
// Java Program to Illustrate Do-while Loop // Classclass GFG {     // Main driver method    public static void main(String args[])    {        // Declaring and initializing integer values        int x = 21, sum = 0;         // Do-while loop        do {             // Execution statements(Body of loop)             // Here, the line will be printed even            // if the condition is false            sum += x;            x--;        }         // Now checking condition        while (x > 10);         // Summing up        System.out.println("Summation: " + sum);    }}
Summation: 176
 
 Related Articles: 
Loops in JavaJava For loop with ExamplesJava while loop with ExamplesDifference between while and do-while loop in C, C++, JavaDifference between for and do-while loop in C, C++, Java
Loops in Java
Java For loop with Examples
Java while loop with Examples
Difference between while and do-while loop in C, C++, Java
Difference between for and do-while loop in C, C++, Java
rishavnitro
java-basics
Java
School Programming
Java
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Split() String method in Java with examples
Stream In Java
Reverse a string in Java
Arrays.sort() in Java with examples
How to iterate any Map in Java
Python Dictionary
Arrays in C/C++
Inheritance in C++
Reverse a string in Java
C++ Classes and Objects