nested for

Nested for loop in Cpp programming language

Nested for loop in Cpp programming language

Nested for loop in C++ language

In this tutorial, We will learn about Nested for loop in CPP programming language

If the for loop contains another for loop inside its body, it is known as nested for loop in C++ language.   Every time, both the outer loop and inner loop should complete their circulation.

 

Declaration

Syntax

for(initialization; test_expression; updating_statement){
         //codes inside the body of outer loop
      for(initialization; test_expression; updating_statement){
           //codes inside the body of outer loop
              }
       }

Here,  there is two for loops, Outer for loop exists inside a for loop known as inner for loop

Explanation of nested for loop in Cpp

Explanation of nested for loop

Flow diagram for nested for loop

Flow diagram for nested for loop

How works nested for loop

Nested for loop has two types of loop

  1. Inner for loop
  2. Outer for loop

Outer for loop executes only once. when its test expression is true. the flow of control moves to inner loop iteration and inner loop executes until its test expression is false.

When the test expression of the inner loop is false, the flow of control skips the execution of the inner loop and come out to execute the outer loop again.

When the test expression of the outer loop is false, the flow of control exit from the loop control and go to rest.

Example

#include <iostream>
#include <conio.h>

using namespace std;

int main()
{
    for(int i=1; i<=3; i++){
    cout << i<<"outer loop executed only once\n" << endl;
    for(int j=1; j<=4; j++){
    cout << j<<"inner loop executed until to complition" << endl;
    }
    cout<<"\n";
    }
getch();
    return 0;
}

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

1 outer loop executed only once

1 inner loop executed until to complition
2 inner loop executed until to complition
3 inner loop executed until to complition
4 inner loop executed until to complition

2 outer loop executed only once

1 inner loop executed until to complition
2 inner loop executed until to complition
3 inner loop executed until to complition
4 inner loop executed until to complition

3 outer loop executed only once

1 inner loop executed until to complition
2 inner loop executed until to complition
3 inner loop executed until to complition
4 inner loop executed until to complition

4 outer loop executed only once

1 inner loop executed until to complition
2 inner loop executed until to complition
3 inner loop executed until to complition
4 inner loop executed until to complition

 

Example

program 1

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

int main()
{
    for(int i=1; i<=10; i++){

      for(int j=1; j<=10; j++){
    cout <<j;
        }
        cout << "\n";
    }
    getch();
    return 0;
}

 

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

12345678910
12345678910
12345678910
12345678910
12345678910
12345678910
12345678910
12345678910
12345678910
12345678910

 

In this program, initially outer for loop start its execution and executes only once. When the test condition is evaluated to be true, the flow of control enters the inner for loop.

Then, the inner for loop completes its execution unless the test condition evaluated to be false.

In that case, the flow of control come out to the outer loop again and executes only once.

Then the flow of control enters the inner loop again.

This circulation should be held until outer loop test expression is false.

When the outer loop test expression is evaluated to be false, the flow of control comes out of the outer loop body and the loop control goes to rest.

Program 2

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

int main()
{
    for(int i=1; i<=10; i++){

      for(int j=1; j<=10; j++){
    cout <<"*";
        }
        cout << "\n";
    }
    getch();
    return 0;
}

 

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

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

 

Nested for loop in the array of c++ language

Program 3

We can use nested for loop to display in two dimension array and the three-dimensional array of elements in C++ language

#include <iostream>
#include <conio.h>
using namespace std;
int main()
{
    int marks[3][4];
    cout<<"Enter the marks for Array \n"<<endl;
    for(int i=0; i<=2; i++){
            for(int j=0; j<=3; j++){

    cout<<"marks["<<i<<"]["<<j<<"]"<<":";
    cin>>marks[i][j];
            }//input marks from user
    }
cout<<"\nYou entered:"<<endl;
    for(int i=0; i<=2; i++){
            for(int j=0; j<=3; j++){
    cout<<"marks["<<i<<"]["<<j<<"]" <<":"<<marks[i][j]<<endl;
        }//output marks
    }
    getch();
    return 0;
}

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

Enter the marks for Array

marks[0][0]:56
marks[0][1]:76
marks[0][2]:78
marks[0][3]:87
marks[1][0]:89
marks[1][1]:98
marks[1][2]:90
marks[1][3]:54
marks[2][0]:45
marks[2][1]:65
marks[2][2]:56
marks[2][3]:73

you entered
marks[0][0]:56
marks[0][1]:76
marks[0][2]:78
marks[0][3]:87
marks[1][0]:89
marks[1][1]:98
marks[1][2]:90
marks[1][3]:54
marks[2][0]:45
marks[2][1]:65
marks[2][2]:56
marks[2][3]:73

 

Program 1

#include <iostream>
#include <conio.h>
using namespace std;
int main()
{
    string Name[2][2][3]; //Array diclaration
    cout<<"Enter your name here\n"<< endl;
    for(int i=0; i<=1; i++){
            for(int j=0; j<=1; j++){
                    for(int k=0; k<=2; k++){
            cout <<"Name["<<i<<"]["<<j<<"]["<<k<<"] :";
            cin>>Name[i][j][k];//Take input from user
                    }
            }
    }
    cout<<"\nyou Entered :"<< endl;
    for(int i=0; i<=1; i++){
            for(int j=0; j<=1; j++){
                    for(int k=0; k<=2; k++){
            cout <<"Name["<<i<<"]["<<j<<"]["<<k<<"] :"<<Name[i][j][k] << endl;//Display names from array
            }
    }
}
getch();
    return 0;
}

 

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

Enter your name here

Name[0][0][0] :Jhon
Name[0][0][1] :Smith
Name[0][0][2] :Nilu
Name[0][1][0] :Suman
Name[0][1][1] :Shagan
Name[0][1][2] :Mackal
Name[1][0][0] :Amir
Name[1][0][1] :Khan
Name[1][0][2] :Bala
Name[1][1][0] :Robert
Name[1][1][1] :Kamith
Name[1][1][2] :Siya

you entered :

Name[0][0][0] :Jhon
Name[0][0][1] :Smith
Name[0][0][2] :Nilu
Name[0][1][0] :Suman
Name[0][1][1] :Shagan
Name[0][1][2] :Mackal
Name[1][0][0] :Amir
Name[1][0][1] :Khan
Name[1][0][2] :Bala
Name[1][1][0] :Robert
Name[1][1][1] :Kamith
Name[1][1][2] :Siya

In the above program, initially, a user can enter the String value to the array for the store using nested for loop.
after we can display elements from the array using nested for loop.

Suggested for you

For loop in Java language

For loop in C++ language

For loop in C language

For loop in Python language

 

 

for loop in Cpp programming language
Floyd's triangle Number pattern using nested for in Java
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.

View Comments

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…

1 month 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