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

Nested do while loop in C

Declaration

Syntax

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

 

Flow diagram of the Nested Do-while loop in C++

Flow diagram – Nested do-while loop

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

Loops in Java language

Loops in C language

Loops in C++ language

 

For loop in C++ language

For loop in Java language

For loop in C language

For loop in Python language

 

While loop in Java language

While loop in C language

While loop in C++ language

While loop in Python language

 

Nested for loop in Java language

Nested for loop in C++ language

Nested for loop in C language

Nested for loop in Python language

 

Nested do while loop in Java programming language
Loops in C programming 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