Nested do while loop in Cpp language
- Home
- Loop
- While loop
- Nested do while loop in Cpp language
- On
- By
- 0 Comment
- Categories: While loop
Nested do while loop in Cpp language
Nested do while loop in Cpp language
In this tutorial, we will learn about Nested do while loop in Cpp language
In C 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 in C++
How to work Nested do while loop
initially, the initialization statement is executed only once and statements(do part) 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 nested do-while loop
program 1
This program displays a square number pattern in C++
#include <iostream> #include <conio.h> using namespace std; int main() { int i,j; i=1; cout<<"Square number pattern\n\n"; cout << "Here your pattern\n" << endl; do{ j=1; do{ cout<<j; j++; }while(j<=10); cout<<"\n"; i++; }while(i<=10); getch(); return 0; }
When the above code is executed, it produces the following results:
Square number pattern Here your pattern 12345678910 12345678910 12345678910 12345678910 12345678910 12345678910 12345678910 12345678910 12345678910 12345678910
Program 2
This program displays a square star pattern using nested- do while loop in C++
#include <iostream> #include <conio.h> using namespace std; int main() { int i,j; i=1; cout<<"Square star pattern\n\n"; cout << "Here your pattern\n" << endl; do{ j=1; do{ cout<<"*"; j++; }while(j<=10); cout<<"\n"; i++; }while(i<=10); getch(); return 0; }
When the above code is executed, it produces the following results:
Square star pattern Here your pattern ********** ********** ********** ********** ********** ********** ********** ********** ********** **********
Program 3
This program displays Floyd’s triangle number pattern using nested- do while loop in C++
#include <iostream> #include <conio.h> using namespace std; int main() { int i,j; i=1; cout<<"Triangle number pattern\n\n"; cout << "Here your pattern\n" << endl; do{ j=1; do{ cout<<j; j++; }while(j<=i); cout<<"\n"; i++; }while(i<=10); getch(); return 0; }
When the above code is executed, it produces the following results:
Triangle number pattern Here your pattern 1 12 123 1234 12345 123456 1234567 12345678 123456789 12345678910
Program 4
#include <iostream> #include <conio.h> using namespace std; int main() { int i,j; i=1; cout<<"Triangle star pattern\n\n"; cout << "Here your pattern\n" << endl; do{ j=1; do{ cout<<"*"; j++; }while(j<=i); cout<<"\n"; i++; }while(i<=10); getch(); return 0; }
When the above code is executed, it produces the following results:
This program displays Floyd’s triangle star pattern using nested- do while loop in C++
Triangle star pattern Here your pattern * ** *** **** ***** ****** ******* ******** ********* **********
Suggested for you
Python program to display the multiplication table
Java program to display the multiplication table
Cpp program to print multiplication table
C program to display multiplication table
Java program to display the multiplication table using array
Similar post
Nested for loop in Java language
Nested for loop in C++ language
Nested for loop in Python language