Skip to main content

Nested Loops In C

Nested Loops In C:


In this tutorial we are going to learn about Nested Loops.
  • What are Nested Loops ?
  • Importance of Nested Loops.
  • Nested Loop Syntax.
  • Programs on Nested Loops.

1. What are Nested Loops ?

  • When a loop is present inside another loop or when multiple loops are present inside a loop then it is called Nested loop.
  • If the outer loop statement is true then the inner loop statement is checked and if it is also true then the code inside the inner loop gets executed.
  • Nested loop is similar to nested if statement the only difference is that a nested loop is executed multiple times till the condition don't becomes false while the nested if statement is executed only once if the condition is true.

2. Importance of Nested Loops:

  • Almost all high level programs involves the use of Nested loops.
  • Nested loops are not limited to just themselves but they are also very important to study the further topic of 2-Dimensional array.
  • Various operations in 2-Dimensional array can only be performed by Nested loops.
  • They Contribute a very important portion in all programming languages.

3. Nested Loop Syntax:

3.1 For for loop:

for( initialization 1, condition 1, operator 1)
for( initialization 2, condition 2, operator 2)
code if both conditions are true;

3.2 For while loop:

int i=1,j=1; ←(initialization 1& 2)
while(i<=10) ←(condition 1)
while(j<=10) ←(condition 2)
code if both conditions are true;
j++; ←(increment 1 or operator 1)
i++; ←(increment 1 or operator 1)


  • Both the loops are initialized according to the requirement with a value.
  • The condition 1 of the outer loop is checked and if it is true then we go inside the loop.
  • The inner loop condition 2 is checked and if it is true then the code written inside the curly brackets gets executed.
  • After that the increment or the operation according to operator 2 of the inner loop takes place.
  • Again the inner loop condition is checked and if it is true the code gets executed again. Again the same process takes place till the condition for the inner loop becomes false.
  • Once the condition for the inner loop becomes false it comes out of the inner loop and the operation for the outer loop that is operator 1 takes place.
  • If the condition is again true after operation the it goes inside the inner loop and again executes the inner loop form its initialization till the condition becomes false.
  • This process goes on and on till the condition for the outer loop becomes false and finally the process ends.
  • So for example if the outer loop is executed 10 times and the inner loop also executes 10 times according to initialization and conditions then the code in the inner loop will be executed 10*10 i.e 100 times.
  • This is because each time we enter the inner loop from outer loop the code is executed 10 times.
  • Example code will make your concept more clear.

4. Example Code:

Q. Write a program to generate prime numbers that occur between two number by accepting numbers from user.

In the last tutorial i.e for loop programs we have learned the concept and code to find out prime numbers the same is going to be used here if you have not red the tutorial yet the links for it is provided at the end  

void main()
int i,j,a,b,c;
printf("\n enter the lowest and the highest numbers");
printf("\n the prime number between %d and %d are",a,b);
printf(" %d,",i);


enter the lowest and the highest numbers
the prime numbers between 1 and 50 are
2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47

Explanation of the above code:

  • In the above code we are just checking whether every single number between 1 and 50 is prime or not and if it is prime we are printing the number.
  • We have declared 5 variables of datatype integer a,b for input, c for counting how many times the number was divisible in the whole loop cycle, i and j for initialization and running the for loop.
  • We first take the input and run the outer for loop from a+1 to b-1 to get each and every number between a and b after that we check the number given by the outer for loop is prime or not using the inner for loop (same concept used as we used to check whether a number is prime or not in the previous tutorial).
  • If the number given by the outer for loop is prime the it gets printed and if its not then it doesn't.


  • Thus we have learned how to use nested loops.
  • Also now we can write a program to generate all prime numbers between two numbers.
If you like the content please share and subscribe.
If you have not seen the previous tutorial of for and while loop programs here is a link to it
If you have any doubts please comment down below.


Popular posts from this blog

C Programming Basics

C PROGRAMMING BASICS: 1. Introduction to C language: C language was invented in Bell Labs by Dennis Ritchie between 1972-73. C language is a case sensitive language i.e the input given to it varies according to upper and lower case letters. It is executed using binary form data such as (0,1). By using binary data the speed of executing a code increases. 1.1 Header Files: Header files are those files which contain some predefined functions in it which we are going to use in our code. Every header file is accessed by appending an extension ".h". Example:               #include<stdio.h>                                 ↓                    ⇡          Tells computer to                  header file  with           include the header                 extension .h          file                                     T here are many header files for different functions such as the following:          <conio.h> <math.h> <string.h

How to start programming for beginners

HOW TO START PROGRAMMING: 1. What is Programming? Programming is a way of communicating with the computer and giving it set of instructions to do the work as per our needs. Example: While calculating something with a calculator what has to be done according to the input given to it is regulated by a program. Programming can also be used to build a software to determine whether a particular number is prime or not. 2. Can anyone learn Programming ? The answer to this question is simple YES anyone with basic knowledge of maths and some common sense can master programming.  The most amazing example of this is Sundar Pichai the CEO of Google he Studied metallurgical engineering at IIT Kharagpur. So if you love it you can do it. 3. Why Should I learn Programming? If you have that passion to solve a problem and enjoy twisting your mind then this is for you else you should leave programming right now. The other important Factor is Money if you become a m

Basic C Programming Questions

Basic Coding Questions: Exercise 1: Q1. Write a programme to calculate area and perimeter of rectangle by accepting its length and breadth. Ans: #include<stdio.h> #include<conio.h> void main() { float a,p,l,b; clrscr(); printf("\n enter the length and breadth of rectangle"); scanf("%f%f",&l,&b); a=l*b; p=2*(l+b); printf("\n the area of rectangle is %f and the perimeter is %f",a,p); getch(); } The above image code is written in code blocks Output: enter the length and breadth of rectangle 20(press enter) 10 (press enter) the area of rectangle is 200 and the perimeter is 60 Explanation of the above code: Declaring of variables in data type float since length and breadth cannot always be integers. Taking input of length and breadth together by using scanf function. Substituting the values of length and breadth stored in l and b