Nested do while loop in Java programming language
- Home
- Loop
- While loop
- Nested do while loop in Java programming language
- On
- By
- 0 Comment
- Categories: While loop
Nested do while loop in Java programming language
Nested do while loop in Java programming language
In this tutorial, we will learn about Nested do while loop in Java programming language
In Java programming language, one do-while loop inside another do-while loop is known as nested do -while loop
Declaration
Syntax
statements do{ statements do{ statements }while(condition); statements }while (condition);
Flow diagram of the Nested Do-while loop
How to work Nested do while loop
initially, the initialization statement is executed only once and statements execute only one. Then, the flow of control evaluates the test expression.
When the test expression is true, the flow of control enters the inner loop and codes inside the body of the inner loop is executed and updating statements are updated. This process repeats until the test expression is false(inner while loop)
Then, when the test expression is false, loop exit from the inner loop and flow of control comes to the outer loop. Again, the flow of control evaluates test condition.
If test condition for outer loop returns as true, the flow of control executes the body of while loop statements and return as false. The flow of control stops execution and goes to rest.
Examples for the nested do-while loop
program 1
class NestedDoWhile{ public static void main(String args[]){ int row=1,column=1; int x; do{ x=4; do{ System.out.print(""); x--; }while(x>=row); column=1; do{ System.out.print(column+" "); column++; }while(column<=5); System.out.println(" "); row++; }while (row<=5); }
This program displays a square number pattern in Java
When the above code is executed, it produces the following results:
12345 12345 12345 12345 12345
Program 2
class NestedDoWhile{ public static void main(String args[]){ int row=1,column=1; int x; do{ x=4; do{ System.out.print(""); x--; }while(x>=row); column=1; do{ System.out.print("*"+" "); column++; }while(column<=5); System.out.println(" "); row++; }while (row<=5); } }
When the above code is executed, it produces the following results:
* * * * * * * * * * * * * * * * * * * * * * * * *
The above program displays a square star pattern in Java using nested while loop
Program 3
This program displays a Floyd triangle number pattern using the nested do-while loop in Java
class NestedDoWhile{ public static void main(String args[]){ int row=1,column=1; int x; do{ x=4; do{ System.out.print(""); x--; }while(x>=row); column=1; do{ System.out.print(row+" "); column++; }while(column<=row); System.out.println(" "); row++; }while (row<=5); } }
When the above code is executed, it produces the following results:
1 2 2 3 3 3 4 4 4 4 5 5 5 5 5
This program displays Floyd’s triangle number pattern using the do-while loop in Java
Program 4
class NestedDoWhile{ public static void main(String args[]){ int row=1,column=1; int x; do{ x=4; do{ System.out.print(""); x--; }while(x>=row); column=1; do{ System.out.print(column+" "); column++; }while(column<=row); System.out.println(" "); row++; }while (row<=5); } }
When the above code is executed, it produces the following results:
1 1 2 1 2 3 1 2 3 4 1 2 3 4 5
This program displays a Floyd triangle number pattern using the do-while loop in Java
Program 5
class NestedDoWhile{ public static void main(String args[]){ int row=1,column=1; int x; do{ x=4; do{ System.out.print(""); x--; }while(x>=row); column=1; do{ System.out.print("*"+" "); column++; }while(column<=row); System.out.println(" "); row++; }while (row<=5); } }
When the above code is executed, it produces the following results:
* * * * * * * * * * * * * * *
This program displays a Floyd triangle star pattern in Java
Suggested for you