Loop Control Statements in C
Introduction to HTML
How to use the Github API
The image tag, anchor tag and the button tag
Ordered and Unordered Lists in HTML
The division tag
HTML Forms
Tables in HTML
Introduction to C Programming
Introduction to Python
Varibles and Datatypes in Python
Operators in Python
Typecasting in Python
Input and Output in Python
If Else in Python
Loops in Python
Break, Continue and Pass in Python
Python practice section 1
Lists in Python
Tuple in Python
Nested loops
Till now, we have learned about using one loop. What about using one loop inside another
loop? Surely, it is possible because we can execute any statement(s) inside the block
of one loop. Let's see how we can do this and why there is a need for nested loops.
- Need for nested loops
- Pattern Printing #1
- Pattern Printing #2
Jump to specific sections
Need for nested loops
The syntax of nested loops is as follows :
Suppose you want to print this square pattern.
* * * * * * * * * * * * * * * * * * * * * * * * *
This is how you would do this -
#include <stdio.h>
void main()
{
for(int i = 1 ; i <= 5 ; i++)
{
printf("* ");
}
printf("\n");
for(int i = 1 ; i <= 5 ; i++)
{
printf("* ");
}
printf("\n");
for(int i = 1 ; i <= 5 ; i++)
{
printf("* ");
}
printf("\n");
for(int i = 1 ; i <= 5 ; i++)
{
printf("* ");
}
printf("\n");
for(int i = 1 ; i <= 5 ; i++)
{
printf("* ");
}
printf("\n");
}
Congrats! It gives the correct results. The first loop prints the first line of the pattern with space.
Then you give a new line and then again write the loop 4 more times to make the full pattern.
In coding, we follow the DRY(Do not Repeat Yourself) principle. We stay away from
writing the same code again and again. Although loops made our life easier, still we had
to write 5 for loops for printing the above pattern. Suppose that the pattern was 1000 lines,
then it would still be a nightmare to copy the same loop again and again. But we are rapid coders!
Nested loops to our rescue! Here, we write our for loop inside the body of another for loop.
In this way, we can execute the for loop any number of times we want. Isn't that amazing? Look at the
below code to see how we implement this.
#include <stdio.h>
void main()
{
for(int i = 1 ; i <= 5 ; i++)
{
for(int j = 1 ; j <= 5 ; j++)
{
printf("* ");
}
printf("\n");
}
}
Output
* * * * * * * * * * * * * * * * * * * * * * * * *
When the value of i = 1, then the inner loop is executed which prints the first line of the pattern.
After that i becomes 2, then the inner loop is executed again which prints the second line of the pattern
, and so on till i = 6.
Pattern Printing #1
Print the following pyramid pattern.
* * * * * * * * * * * * * * *
Code
#include <stdio.h>
void main()
{
for(int i = 1 ; i <= 5 ; i++)
{
for(int j = 1 ; j <= i ; j++)
{
printf("* ");
}
printf("\n");
}
}
Explanation
For the first line, i = 1 and the inner loop should be run exactly once to print only one *. For the
second line, i = 2 and the inner loop should be run 2 times to print * * and so on for all the lines.
Hence, we execute the inner loop from 1 to i times to print the above pattern.
Pattern Printing #2
Print the following inverted pyramid pattern.
* * * * * * * * * * * * * * *
Code
#include <stdio.h>
void main()
{
for(int i = 5 ; i >= 1 ; i--)
{
for(int j = 1 ; j <= i ; j++)
{
printf("* ");
}
printf("\n");
}
}
Explanation
For the first line, i = 5 and the inner loop should be run 5 times to print * * * * *. For the
second line, i = 4 and the inner loop should be run 4 times to print * * * * and so on for all the lines.
Hence, we execute the inner loop from 1 to i times to print the above pattern. Here, we executed the
outer loop from 5 to 1 by decrementing the value of i instead of going from 1 to 5 in order to make the pyramid inverted!