Nested Loops in C Programming Language
Nested Loops in C Programming Language
In the previous blog posts we have studied about the loops, and different types of loops in C programming language. We know that loops are used to iterate or repeat a piece of code as per the requirement of our programming logic. And one of the main advantages of using these looping statements is that it increases code reusability and also enhances the performance of the program or code.
Tody we are going to cover the nested loops in C programming language. We are going to look at that how can we nest a loop inside another loop. Now just keep one thing in mind that we can nest any loop inside any other looping statement. In other words we can say that we can place one loop statement inside the body of another loop statement.
Now there is another very important point which is, when we use nested loops the inner loop completes all of its iterations for every single iteration of the outer loop. If outer loop runs M times and inner loop will runs N times, then the inner loop will run or execute a total of M x N times. If this point is not clear to you, dont worry we are going to cover some C programming coding examples that will help you out in understanding this concept more clearly.
We know that there are three main looping statements which are
1) for loop
2) while loop
3) do while loop
At this point keep one thing in mind that we can use for loop inside another for loop, while loop, or do while loop. So in simple words we can say that we can use any looping statement inside any other looping statement like, a for loop inside a while loop or while loop inside a for loop.
we have seen loop implementation one at a time i.e., one loop type implemented in the program. Now we will look at how to use the nested loops in the C programming language. The first thing to keep in mind is that, what we mean by the term nested loop. The main idea behind nested loops is to place a loop inside another loop.
In this article, we are going to look at nested for loops in the C programming language. As we previously discussed we can use a loop inside another loop. So today we are going to look at how we can use a for loop inside another for loop.
Remember that if we have two for loops one for loop inside another for loop, and if both the loops are set to three iterations, then for each iteration of the outer loop the inner loop will execute three times i.e. if the outer loop runs three times the inner loop will run the total of 9 times let us look at the syntax of nested loop.
Basic Syntax and working of nested loops in C programming language
First Syntax
outer_loop { inner_loop { // inner loop statements. } // outer loop statements. }In the above C programming coding syntax we can see that we have created or used two loops one is named as outer_loop and the other one is named as inner_loop. So this is the moost simplest and basic syntax example with respect to nested loops. And the working of nested loops is simple, When the inner_loop runs inside the body of the outer_loop, the inner_loop will run the complete cycle of its iterations.
Second Syntax
for (initialization; condition; increment) { for (initialization; condition; increment) { // inner loop statements. } // outer loop statements. }
After that we have nested loop syntax of for loop in C progamming language. And if we look at this nested for loop syntax example, we have used two for loops one is the outer for loop and the second one is the inner for loop. The working of the nested for loops is also same when outer for loop runs one time, the inner for loop will run complete cycle of its iterations. Suppose if both the for loops are going to run 3 times. Then for every iteration of the outer for loop the inner for loop will complete its 3 iterations. Which means that when the outer for loop runs 3 times the inner for loop has run total number of 9 times.
Third Syntax
for (initialization; condition; increment) { while (condition) { // inner loop statements. } // outer loop statements. }
In the third syntax example of nested loops we are showing that we can use while loop inside a for loop.
Simple coding example of nested loops in C programming language
Nested for loop example
#include <stdio.h> void main() { for (int i = 1; i <= 3; i++) { for (int j = 1; j <= 3; j++) { printf("%d\t", i * j); } printf("\n"); } _getch(); }
Explanation of above program:
In the above example, there are two for loops, one for loop inside another for loop. So this is a nested for loop. If we look at this program the outer for loop has variable i and it will be executed 3 times because i is equal to 1 and the loop will run until i becomes less or equal to 3 according to the outer for loop condition i.e.
i <= 3
And also the inner loop will be executed 3 times as we look at variable j=1 and j<=3. But keep this important point in mind for every outer loop iteration the inner loop will run 3 times i.e. if the outer for loop with variable i will run 3 times then for every outer for loop iteration the inner for loop will run 3 times. So the total number of times the inner for loop will be executed is 9 times.
Thus, the execution of this simple program will result in printing 9 numbers with 3 rows and 3 columns. The output of this program would be something like this,
Output of the above code
Congratulations! like always we have successfully implemented the nested loops in C programming language.
Comments
Post a Comment