Flowchart of Java do-while Loop
Example 1 : Java do-while Loop Program
Output
javabytechie.com
javabytechie.com
javabytechie.com
javabytechie.com
javabytechie.com
Java Nested do-while Loop:
Java also allows to write one do-while loop inside another do-while loop, it is knowns as Nested do-while loop or Inner do-while loop in Java.
Syntax of Nested do-while loop in Java
Now, let's understand Nested do-while loop through the given example.
Example 2 : Java Nested do-while Loop
Output
Outer do-while loop
Inner do-while loop
Inner do-while loop
Inner do-while loop
Inner do-while loop
Outer do-while loop
Inner do-while loop
Inner do-while loop
Inner do-while loop
Inner do-while loop
Outer do-while loop
Inner do-while loop
Inner do-while loop
Inner do-while loop
Inner do-while loop
Infinite do-while Loop in Java
A loop that does not terminate that loop is known as an Infinite loop in Java.
Example 3 : Infinite do-while Loop in Java
Output
It's an Infinite Loop
It's an Infinite Loop
It's an Infinite Loop
It's an Infinite Loop
It's an Infinite Loop
It's an Infinite Loop
It's an Infinite Loop
It's an Infinite Loop
...
...
...
Continue and Break statements inside the do-while loop
We can use continue and break statements inside the do-while loop according to our requirements.
In Java, the continue statement is mainly used inside loops such as for loops, while loops, do-while loops, etc. The purpose of the continue statement is to continue the current running flow of the Java program and skip the rest of the code at the specified condition.
A break statement is used to terminate the current processing of the loop. After the termination, control returns from the loop immediately to the next statement after the loop.
Let's understand the below Java program where we are iterating a loop from 1 to 30 but printing the even value from 11-20.
Example 4 : Continue and Break Statements inside do-while Loop
Output
Even Number: 12
Even Number: 14
Even Number: 16
Even Number: 18
Even Number: 20