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
Syntax
statements do{ statements do{ statements }while(condition); statements }while(condition);
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.
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
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…