The do-while Loop (Part 5)
Do-while Loop
The do-while
loop is a variant of the while
loop. This loop will execute the code block once, before checking if the condition is true, then it will repeat the loop as long as the condition is true.
The do-while loop is a exit controlled loop , as it will run about the first time without even checking the condition and afterwards the do statement will be executed as long as the while statement condition remains true.
Syntax:-
do {
// code block to be executed
}
while (condition);
Flowchart:-
Eg:-
public class MyClass {
public static void main(String args[]) {
int num = 1;
String str = "Hello";
do {
System.out.println(str + " World");
num++;
} while (num <= 5);
}
}
Output:-
Hello World
Hello World
Hello World
Hello World
Hello World