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 nested 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 nested 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 using the nested do-while loop in Java

There are other Java language keywords that are similar to this post

While keyword in Java

do keyword in Java

for keyword in Java

 

Suggested for you

Nested for loop in Java language

Nested for loop in C++ language

Nested for loop in C language

Nested for loop in Python language

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

PHP Star Triangle pattern program

PHP Star Triangle pattern program In this tutorial, we will discuss about PHP Star Triangle…

1 month ago

PHP Full Pyramid pattern program

PHP Full Pyramid pattern program In this tutorial, we will discuss about PHP Full Pyramid…

2 months ago

5 methods to add two numbers in Java

5 methods to add two numbers in Java In this tutorial, we will discuss the…

2 months ago

Python Full Pyramid star pattern program

Python full Pyramid star pattern program In this tutorial, we will discuss  the concept of…

5 months ago

Write a function or method to convert C into F -Entered by user

Write a function or method to convert C into F -Entered by the user In…

10 months ago

How to write a function or method to convert Celsius into Fahrenheit

How to write a function or method to convert Celsius into Fahrenheit In this tutorial,…

10 months ago