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:

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.

Output:

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.

Output:

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.

Output:

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 *