Java do-while loop with Examples – GeeksforGeeks
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 statementoutput 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 <= 10B. 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
- 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
- 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 Worldauxiliary space : oxygen ( one )
Output explanation: The plan bequeath perform inch the follow manner american samoa stick to :
- 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 time
- Updation is done. Now i = 4.
- Condition is checked. 4 < 6 yields true.
- Execution enters the loop
- “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 time
- Updation is done. Now i = 6.
- Condition is checked. 6 < 6 yields false.
- 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: 176Example 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