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
Syntax
statements do{ statements do{ statements }while(condition); statements }while (condition);
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.
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
C# inverted full pyramid star pattern In this article, we will discuss the concept of…
C# Full Pyramid star pattern program In this article, we will discuss the concept of…
Program to count vowels, consonants, words, characters and space in Java In this article, we…
How to print multiplication table using Array in C++ language In this post, we will…
C Program to multiplication table using Array In this tutorial , we will discuss about…
Java program to check odd or even using recursion In this tutorial, we discuss a…