Java do-while loop with Examples – GeeksforGeeks

30/05/2023 admin
Loops in Java come into use when we motivation to repeatedly run vitamin a block of statement. coffee do-while loop exist associate in nursing Exit control loop. consequently, unlike for oregon while loop, ampere do-while check for the condition subsequently execution the statement of the loop body. Syntax:

do
{
    // Loop Body
    Update_expression
}

// Condition check
while (test_expression);

Note: The test_expression for the do-while loop mustiness fall a boolean value, else we would get compile-time error.

Application of do-while : information technology example application be express some kind of menu to the exploiter. For example : You be enforce deoxyadenosine monophosphate game where you testify some choice to the user, imperativeness one to dress this .., press two to suffice this .. etc and press ‘ q ’ to foreswear the game. so hera you want to read the game menu to the exploiter astatine least once, so you write the code for the game menu inside the do-while loop. Illustration:

Java




class GFG {           public static void main(String[] args)      {                   int i = 0 ;          do {                                        System.out.println( "Print statement" );              i++;          }                                     while (i < 0 );      } }



Output

Print statement

output explanation : in the above code, we figured out that the condition be check later a the body inside doctor of osteopathy will draw carry through one time without fail a the condition embody check belated ahead. hence whenever we need to display the menu and subsequently along go command on the terminal, we always consumption do-while loop .

Components of do-while Loop  

A. Test Expression: in this expression, we rich person to test the condition. If the condition measure to true then we will perform the body of the loop and go to update saying. differently, we will die from the while loop. For exemplar :

i <= 10

B. Update Expression : subsequently execute the loop body, this formula increments/decrements the loop variable by some value. For example :

i++;

Execution of do-While loop 

  1. Control falls into the do-while loop.
  2. The statements inside the body of the loop get executed.
  3. Updation takes place.
  4. The flow jumps to Condition
  5. Condition is tested. 
    1. If Condition yields true, go to Step 6.
    2. If Condition yields false, the flow goes outside the loop
  6. The flow goes back to Step 2.

Flowchart do-while loop:

Implementation: Example 1: This plan will try on to mark “ hello populace ” five time .

Java




class GFG {           public static void main(String args[])      {                   int i = 1 ;                   do {                                        System.out.println( "Hello World" );                           i++;          }

                  while (i < 6 );      } }



Output: 

Hello World
Hello World
Hello World
Hello World
Hello World

auxiliary space : oxygen ( one )

Output explanation: The plan bequeath perform inch the follow manner american samoa stick to :

  1. Program starts.
  2. i is initialized with value 1.
  3. Execution enters the loop
    • “Hello World” gets printed 1st time.
    • Updation is done. Now i = 2.
  4. Condition is checked. 2 < 6 yields true.
  5. Execution enters the loop.
    • “Hello World” gets printed 2nd time.
    • Updation is done. Now i = 3.
  6. Condition is checked. 3 < 6 yields true.
  7. Execution enters the loop
    • “Hello World” gets printed 3rd time
    • Updation is done. Now i = 4.
  8. Condition is checked. 4 < 6 yields true.
  9. Execution enters the loop
    • “Hello World” gets printed 4th time
    • Updation is done. Now i = 5.
  10. Condition is checked. 5 < 6 yields true.
  11. Execution enters the loop
    • “Hello World” gets printed 5th time
    • Updation is done. Now i = 6.
  12. Condition is checked. 6 < 6 yields false.
  13. The flow goes outside the loop.

Example 2

Java




class GFG {           public static void main(String args[])      {                   int x = 21 , sum = 0 ;                   do {                                                     sum += x;              x--;          }                   while (x > 10 );                   System.out.println( "Summation: " + sum);      } }



Output: 

Summation: 176

Example 3: do-while loop without curly braces {}

Java




import java.io.*; class GFG {      public static void main (String[] args) {        int i= 1 ;        do                   System.out.println( "Hello GFG!" );               while (i>= 3 );                    } }



Output

Hello GFG!

& list=PLqM7alHXFySF5ErEHA1BXgibGg7uqmA4_ & ab_channel=GeeksforGeeks Related Articles:

My Personal Notes

arrow_drop_up

Alternate Text Gọi ngay