C++ Nested for Loop

In any programming language loops play an important role in building our logic and successfully implementing it. In this article, we are going to see nested for loop in C++.

Nested for loop means a for loop inside another for loop. Let’s see the syntax first and after that, we will some examples.

Syntax:

for ( size_t i = 0; i < count; i++ )
    {
        for ( size_t j = 0; j < count ; j++ )
        {
            /* code */
        }
    }

Here we can see the syntax of nested for loop. We can modify the inner contents as per our wish but the main structure of the for loop should remain the same.

Let’s see some examples of nested for loop.

Example 1:

In the first example, we are going to print a 5 x 4 matrix using a nested for loop, which we often see in mathematics.

#include <iostream>
using namespace std; 

int main(){

    int n = 5;
    int m = 4;

    for (int i = 0; i < n; i++){       // printing the row
        for (int j = 0; j < m; j++)    // printing the column
        {
            cout << j << " ";
        }
        cout << endl;

    }
    return 0;
}

Output:

PS C : \ Users \ ASUS \ Desktop \ Crazy Programmer Work > cd " c : \ Users \ ASUS \ Desktop \ Crazy Programmer Work \ "  ; if ($?) { g++ test.cpp -o test } ; if ($?) { . \ test }
0 1 2 3
0 1 2 3
0 1 2 3
0 1 2 3
0 1 2 3

Example 2:

In this example, we will be going to see how to take inputs using nested for loops and we will store the inputs in 2 dimension arrays. After that, we will print the 2 dimension array using nested for loop.

#include <iostream>
using namespace std;

int main(){

    int row = 3;
    int col = 3;
    int arr[3][3];
    int num;

    // Taking matrix input
    cout << "Enter Matrix Elements" << endl;
    for (int i = 0; i < row; i++){
        for (int j = 0; j < col; j++){
            cin >> num;
            arr[i][j] = num;
        }
    }

    // Printing the matrix
    cout << "Printing the Matrix : " << endl;
    for (int i = 0; i < row; i++){         // printing the row
        for (int j = 0; j < col; j++){     // printing the column
            cout << arr[i][j] << " ";
        }
        cout << endl;
    }
    return 0;
}

Output:

PS C : \ Users \ ASUS \ Desktop \ Crazy Programmer Work > cd " c : \ Users \ ASUS \ Desktop \ Crazy Programmer Work \ "  ; if ($?) { g++ test.cpp -o test } ; if ($?) { . \ test }
Enter Matrix Elements
1 2 3
4 5 6
7 8 9
Printing the Matrix :
1 2 3
4 5 6
7 8 9

We can take inputs for any matrix using nested for loop. It helps us to easily demonstrate the internal working of how input is taken and stored in the array.

We can also write any conditions in the nested for loop, for example, let’s suppose we want to print only even numbers from the matrix or we want to print only odd numbers from the matrix.

We can also use break and continue statements in our nested loop.

If the break statement is in the inner for loop then it only stops the execution for the inner for loop. It will not affect the outer for loop.

The same happens with the continue statement also, if we have used the continue statement in the inner for loop then it only affects the inner loop, it has nothing to do with the outer for loop.

Example 3:

In this example we are going to see the use of break statement and continue statement in nested for loop.

Let’s suppose we want that while printing the array, if a number is negative then our loop stops and if a number is 0 then our loop excludes the number while printing.

Make sure to see the serial wise execution of the program, because in this example we are going to see the use of break and continue statements simultaneously.

#include <iostream>
using namespace std;

int main(){

    int row = 3;
    int col = 3;
    int arr[3][3];
    int num;

    // Taking matrix input
    cout << "Enter Matrix Elements" << endl;
    for (int i = 0; i < row; i++){
        for (int j = 0; j < col; j++){
            cin >> num;
            arr[i][j] = num;
        }
    }

    // Printing the matrix
    cout << "Printing the Matrix : " << endl;
    for (int i = 0; i < row; i++){   // printing the row
        for (int j = 0; j < col; j++){   // printing the column
            // break the loop if a number is negative
            if (arr[i][j] < 0)
		{
                break;
            }
            // Do not print the element if it is 0
            if (arr[i][j] == 0)
		{
                continue;
            }
            cout << arr[i][j] << " ";
        }
        cout << endl;
    }
    return 0;
}

Output:

PS C : \ Users \ ASUS \ Desktop \ Crazy Programmer Work > cd " c : \ Users \ ASUS \ Desktop \ Crazy Programmer Work \ "  ; if ($?) { g++ test.cpp -o test } ; if ($?) { . \ test }
Enter Matrix Elements
10 20 -54
14 0 -12
-18 87 0
Printing the Matrix :
10 20
14

As we can see in the output, we have used break and continue statement in the program, break statements stop the execution of inner loop that why in the case of third row we did not get any output.

Conclusion

Nested for loop means that a for loop in another for loop. We have seen the syntax of nested for loop and we have seen 3 examples on it. We have seen how to take inputs in nested for loop and print that input. Nested for loop is very useful when we are working with any type of matrices or we are trying to print any patterns. Nested for loop will be useful when we are working with any sorting algorithms.

1 thought on “C++ Nested for Loop”

  1. Tiberiu Zbîrnea

    Hi!
    I’m following your articles for some years and you’re doing a great job.
    Just a small opinion:
    where you intro the syntax both loops are on same variable: i.
    Can confuse some compilers.
    Have a nice day!

Leave a Comment

Your email address will not be published. Required fields are marked *