Java - do-while Loop
Java provides do-while loop which is one of the control flow statements and it will execute
the block of code at-least one or more time as long as the specified conditional expression is valid
(i.e., true)
Example: #1 - Simple while Loop
Print the number from 1 to 5 using do-while loop
Java Input Screen
class ControlFlowStatement {
public static void main(String[] args) {
int counter = 1;
do {
System.out.println(counter);
// Increment the number by 1 using Post Increment
counter++;
} while(counter <= 5);
}
}
Example: #2 - do-while Loop (execute once and terminate)
Print the number from 1 to 5 in the console. In below example the do-while loop will run once without
checking any condition and print the counter variable value. Then it will check the condition
while(counter <= 5) which is not true
so the loop execution will get terminated.
Java Input Screen
class ControlFlowStatement {
public static void main(String[] args) {
int counter = 10;
System.out.println("Execution before while loop");
do {
System.out.println(counter);
// Increment the number by 1 using Post Increment
counter++;
} while(counter <= 5);
System.out.println("Execution after while loop");
}
}
Java Output Screen
Execution before while loop
10
Execution after while loop
Thousands Lived without Love, but not without water. So SAVE WATER.
Example: #3 - do-while Loop (Infinite loop)
Print the number form 1 to 5, the do-while loop has the valid conditional expression but the code
inside the do block doesn't have any statement to increment the counter variable.
So the conditional expression won't reach the termination in otherwords it loops endlessly.
Java Input Screen
class ControlFlowStatement {
public static void main(String[] args) {
int counter = 1;
do {
System.out.println(counter);
} while(counter <= 5);
}
}
Thousands Lived without Love, but not without water. So SAVE WATER.
Java do-while Loop with break Statement
The break statement is a termination statement, once the
break statement encountered
in a do-while loop, immediately it will terminate the
looping execution and it return backs to the main statement execution.
Example: #4 - do-while Loop with break Statement
Terminate the do-while loop once the console printed the number 3
Java Input Screen
class ControlFlowStatement {
public static void main(String[] args) {
int counter = 1;
do {
System.out.println(counter);
// Terminate the execution once counter reached 3
if(counter == 3) {
break;
}
// Increment the number by 1 using Post Increment
counter++;
} while(counter <= 5);
}
}
Thousands Lived without Love, but not without water. So SAVE WATER.
Java do-while Loop with continue Statement
The continue statement is a skipping statement of a current iteration, once the continue
statement encountered in a while loop, immediately it will skip the current iteration and
it will continue the rest of the operation till the conditional expression is true.
Example: #5 - do-while Loop with continue Statement
Skip the do-while loop once the counter has reached the value 3 using the
continue statement.
Java Input Screen
class ControlFlowStatement {
public static void main(String[] args) {
int counter = 0;
do {
// Increment the number by 1 using Post Increment
counter++;
// skips the iteration once counter reached 3
if(counter == 3) {
continue;
}
System.out.println(counter);
} while(counter <= 5);
}
}
BBMINFO