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

Nested do while loop in Java

Declaration

Syntax

statements
do{
statements
do{
statements
}while(condition);
statements
}while (condition);

Flow diagram of the Nested Do-while loop

Flow diagram – 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

for loop in Java

while loop in Java

for loop in C language

while loop in C language

while loop in cpp language

For loop in Cpp language

 

Nested for loop in C++

Nested for loop in C

Nested for in  Python

Do while loop in Cpp programming language
Nested do while loop in Cpp language
Karmehavannan

I am Mr S.Karmehavannan. Founder and CEO of this website. This website specially designed for the programming learners and very especially programming beginners, this website will gradually lead the learners to develop their programming skill.

Recent Posts

C# inverted full pyramid star pattern

C# inverted full pyramid star pattern In this article, we will discuss the concept of…

3 weeks ago

C# Full Pyramid star pattern program

C# Full Pyramid star pattern program In this article, we will discuss the concept of…

1 month ago

Program to count vowels,consonants,words, characters and space in Java

Program to count vowels, consonants, words, characters and space in Java In this article, we…

1 month ago

How to print multiplication table using Array in C++ language

How to print multiplication table using Array in C++ language In this post, we will…

1 month ago

C Program to multiplication table using Array

C Program to multiplication table using Array In this tutorial , we will discuss about…

2 months ago

Java program to check odd or even using recursion

Java program to check odd or even using recursion In this tutorial, we discuss a…

2 months ago