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
printf("\n enter the lowest and the highest numbers");
printf("\n the prime number between %d and %d are",a,b);
enter the lowest and the highest numbers
the prime numbers between 1 and 50 are
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.