Nested loops in C

Here you will learn about nested loops in C.

Nesting is one of the most complex topics of C programming. Just like decision control instructions, a loop control instruction can also be nested easily. But sometimes understanding the execution of such programs could be a bit tricky. So it is important to make the basics clear.

Nested loops in C

As I said in my earlier tutorials, nesting means defining statement under the scope of another similar statement. In case of loops, when we nest two loops then it generally multiplies the execution frequency of loops.

We can nest for loop inside while loop and vice versa is also true. Generally programmer nest up to 3 loops. But there is no limit of nesting in C.

Now let’s try a small program with nested loops.

#include<stdio.h>

void main()
{
 int row,col;
 for(row=1;row<4;row++)
 {
  for(col=1;col<4;col++)
  {
   printf("%d\t%d\n",row,col);
  }
 }
}

Output

Nested loops in C - Output

Explanation

  • Every row executes a column at least 3 times. Therefore you are seeing all three values with every row.
  • Once the control reaches inside the inner loop, it came out only when the loop is fully executed.
  • We cannot alter the control once it reaches inside the inner loop.

Note: I have deliberately shown a simple program to show nesting of loops. Execution of nested loops can’t be summarized in words. So it will be better if you try your hands on the nesting of loops to understand them better.

Odd Loop

There are situations when we need to execute a set of instructions until the user deny it. So we need to check if the user wants to repeat the set of instructions or not.

In that case programmer has no idea about the executing time and frequency of the program. This is called odd loop in C programming. So basically we have to make a program which will ask to the user about the re-execution of program.

#include<stdio.h>

void main()
{
 char yes='Y';
 int x,y;
 
 while(yes=='Y')
 {
  printf("Enter two values to perform additionn");
  scanf("%d%d",&x,&y);
  printf("Sum is %d n",x+y);
  printf("Do you want to continue ? Press Y for Yes and N for Non");
  scanf("%c", &yes);
 }
}

Output

Nested loops in C - Output

Explanation

  • In the beginning I have character variable yes with character initial value ‘Y’. After that I have declared two more integer variables.
  • Now I have started while loop with condition yes==’Y’. It means the loop will be executed until it receives value ‘Y’.
  • After that I have displayed the message to enter two values. By using scanf() function I have stored data inside the variables.
  • Now instead of using a third variable to calculate sum. I have used a simple argument to calculate sum inside printf() function.
  • In the end the program will display the message for user. If he wants to continue then he will press “Y”. Otherwise he will press “N” and the program will be terminated.

Comment below if you have doubts or found any information incorrect in above tutorial for nested loops in C.

5 thoughts on “Nested loops in C”

Leave a Comment

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