Codeforcoding

Nested while loop in Cpp programming language

Nested while loop in Cpp programming language

In this tutorial, We will learn about Nested while loop in Cpp programming language

Nested while loop in Cpp programming language
Nested while loop in C++

Nested while loop

While loop inside the body of another while loop is known as  Nested while loop in C++ programming language.

one iteration of the outer loop initially executed before the inner loop begins to execute. but the execution of the inner loop continues until the condition of the inner loop is satisfied(until the test expression is false).

The condition of the inner loop is satisfied flow of control come out to again outer loop for next iteration

Declaration

Syntax

while(test_expression_1)
           {
             statement(s)
                 while(test_expression_2)
                                {
                                 statement(s)
                               }
           }

Flow diagram for the nested while loop

Flowchart for the nested while loop

The flow of nested while loop in C++

#include <iostream>
#include <conio.h>
using namespace std;

int main()
{
    int i=0;
    while(i<=3){
        cout<<"outer loop executes"<< endl;
        int j=0;
        while(j<=4){
            cout<<"inner loop executes";
            cout << "i = "<<i<<" and j="<<j << endl;
        j++;
        }
        i++;
    }
    getch();
    return 0;
}

When the above code is executed, it produces the following results:

Outer loop executes
Inner loop executes i=0 and j=0
Inner loop executes i=0 and j=1
Inner loop executes i=0 and j=2
Inner loop executes i=0 and j=3
Inner loop executes i=0 and j=4
Outer loop executes
Inner loop executes i=0 and j=0
Inner loop executes i=0 and j=1
Inner loop executes i=0 and j=2
Inner loop executes i=0 and j=3
Inner loop executes i=0 and j=4

Outer loop executes
Inner loop executes i=0 and j=0
Inner loop executes i=0 and j=1
Inner loop executes i=0 and j=2
Inner loop executes i=0 and j=3
Inner loop executes i=0 and j=4

Outer loop executes
Inner loop executes i=0 and j=0
Inner loop executes i=0 and j=1
Inner loop executes i=0 and j=2
Inner loop executes i=0 and j=3
Inner loop executes i=0 and j=4

Above the example initially, the outer loop is executed only once then inner loop executes until the test condition becomes false and same time display output.

The outer loop executes again one time and then continuously executes inner loop

This circulation  is running until outer loop returns false

when the outer loop becomes the false flow of control skip the execution and jump out of the loop and end the loop execution

The example in the nested while loop in C++

Program 1

#include <conio.h>
using namespace std;
int main()
{
    int i=1;
    int j;
    while(i<=6)
    {
        j=1;
        while(j<=6){
    cout <<j;
    j++;
}
    cout<< endl;
    i++;
    }
    getch();
    return 0;
}

 

When the above code is executed, it produces the following results:

We can see three cases in this program

case 1

When we print j it produces the following results:

123456
123456
123456
123456
123456
123456

 

 

case 2

When we print i it produces the following results:

111111
222222
333333
444444
555555
666666

 

 

case 3

When we print “*” as a string it produces the following results:

******
******
******
******
******
******

 

Nested while loop with two dim array

The display element of two dimension Array

We can display elements from two dimension array in C++, using nested while loop

Program 1

#include <iostream>
#include <conio.h>
using namespace std;

int main()
{
    string arr[3][4]={
    {"Niru","Saru","Pithu","Suthu"},
    {"Kuru","Hari","Ram","Sam"},
    {"Nilani","Roja","Mala","Mani"},
    };
    int i=0;
    while(i<=2){
        int j=0;
    while(j<=3){
      cout << arr[i][j] << endl;
      j++;
    }
    i++;
    }
    cout << "End the program" << endl;
    getch();
    return 0;
}

 

When the above code is executed, it produces the following results:

Niru
Saru
Piyhu
Suthu
Hari
Ram
Sam
Nilani
Roja
Mala
Mani
End of the program

You can input element to the array and get output from an array using nested while loop

#include <iostream>
#include <conio.h>
using namespace std;
int main()
{
    int num[2][3];
    cout <<"Enter element for array"<< endl;
    int i=0;
    while(i<=1){
        int j=0;
        while(j<=2){
            cout <<"num["<<i<<"]["<<j<<"]=";
            cin>>num[i][j];
            j++;
                }
                i++;
    }
    cout << "\n your entered :" << endl;
    int k=0;
    while(k<=1){
        int l=0;
        while(l<=2){
            cout <<"num["<<k<<"]["<<l<<"]="<<num[k][l]<< endl;
            l++;
        }
        k++;
    }
    cout << "End of program" << endl;
    getch();
    return 0;
}

When the above code is executed, it produces the following results:

Enter element for array
num[0][0]=35
num[0][1]=47
num[0][2]=58
num[1][0]=72
num[1][1]=60
num[1][2]=94

you entered
num[0][0]=35
num[0][1]=47
num[0][2]=58
num[1][0]=72
num[1][1]=60
num[1][2]=94
End of program

 

Suggested for you

While loop in Java

While loop in C++

While loop in C

While loop in Python

Nested while loop in C programming language
Nested while loop in Python programming language
Exit mobile version