C Nested Loops
In this tutorial, we will learn about nested loops in C with the help of examples. We will also learn about break and continue in Nested Loop
Introduction of Nested Loop in C
A loop within another loop is called a nested loop. Nested loop means a loop statement inside another loop statement. That's why nested loop are also called as loop inside loop.
Working of Nested Loop
- Execution of statement within the loop flows in a way that the inner loop of the nested loop gets
declared, initialized and then incremented
. - Once all the condition within the inner loop gets satisfied and becomes true it moves for the search of the outer loop. It is often called a
loop within a loop
.
Let's take an example:-
Suppose we want to loop through each day of a week for 3 weeks. To achieve this, we can create a loop to iterate three times (3 weeks). And
inside the loop, we can create another loop to iterate 7 times (7 days).
This is how we can use nested loops.
Nested for Loop
A for loop within another for loop is called Nested For loop
The syntax of nested for loop is:
for (initialization; condition; update)
{
for (initialization; condition; update)
{
// body of inner for-loop
}
// body of outer for-loop
}
Nested while Loop
A while loop within another while loop is called Nested while loop.
The syntax of nested while loop is:
while (condition)
{
while (condition)
{
// body of inner while-loop
}
// body of outer while-loop
}
Nested do-while Loop
A do-while loop within another do-while loop is called Nested do-while loop.
The syntax of nested do-while loop is:
do
{
do
{
// body of inner do-while-loop
}while (condition);
// body of outer do-while-loop
}while (condition);
Example 1: C Nested for loop
// C program to display 7 days a weeks
#include <stdio.h>
int main() {
int weeks = 1, days_in_week = 7;
for (int i = 1; i <= weeks; ++i) {
printf("Week: %d\n", i);
for (int j = 1; j <= days_in_week; ++j) {
printf(" Day: %d\n", j);
}
}
return 0;
}
Output
Week: 1 Day:1 Day:2 Day:3 Day:4 Day:5 Day:6 Day:7
We can create nested loops with while and do...while in a similar way.
Example 2: Displaying a Pattern
// C program to display a triangular pattern
// Number is entered by the user
#include <stdio.h>
int main() {
int i, j, n;
printf("Enter Number : ");
scanf ("%d", &n);
for (i = 1; i <= n; i++) {
for (j = 1; j <= i; j++) {
printf("* ");
}
printf("\n");
}
return 0;
}
Output
Enter Number : 4 * * * * * * * * * *
In this program, the outer loop iterates from 1
to rows.
The inner loop iterates from 1
to columns. Inside the inner loop, we print the character '*'
.
Example 3: C Nested while Loop
// C program to display a triangular pattern of numbers using nested while loop
#include <stdio.h>
int main() {
int rows, i = 1;
printf("Enter Number of rows : ");
scanf ("%d", &rows);
while (i <= rows) {
int j = 1;
while(j <= i) {
printf("%d", i);
j++;
}
printf("\n");
i++;
}
return 0;
}
Output
Enter the number of rows: 4 1 22 333 4444
In this program, the outer loop iterates from 1
to rows.
The inner loop iterates from 1
to i. Inside the inner loop, we print the numbers.
Example 4: C Nested do-while Loop
// C program to display a triangular pattern of numbers using nested do-while loop
#include <stdio.h>
int main() {
int rows, i,j;
i = 1;
printf("Enter the number of rows: ");
scanf ("%d", &rows);
do {
j = 1;
do{
printf("%d", j);
j++;
}while(j <= i);
printf("\n");
i++;
}while(i <= rows);
return 0;
}
Output
Enter the number of rows: 4 1 12 123 1234
break and continue Inside Nested Loops
When we use a break statement inside the inner loop, it terminates the inner loop but not the outer loop. For example,
Example 4: C break Inside Nested Loops
// C program to display a triangular pattern of numbers using nested do-while loop
#include <stdio.h>
int main() {
int weeks = 3, days_in_week = 2;
for (int i = 1; i <= weeks; ++i) {
printf("Week: %d\n", i);
for (int j = 1; j <= days_in_week; ++j) {
// break during the 2nd week
if (i == 2) {
break;
}
printf (" Day:%d\n", j );
}
}
return 0;
}
Output
Week: 1 Day:1 Day:2 Week: 2 Week: 3 Day:1 Day:2
This program does not run the inner loop when the value of i is 2
i.e. it does not print the days of the 2nd week. The outer loop that prints the weeks is unaffected.
Similarly, when we use a continue statement inside the inner loop, it skips the current iteration of the inner loop only. The outer loop is unaffected. For example,
Example 4: C break Inside Nested Loops
// C program to display a triangular pattern of numbers using nested do-while loop
#include <stdio.h>
int main() {
int weeks = 3, days_in_week = 7;
for (int i = 1; i <= weeks; ++i) {
printf("Week: %d\n", i);
for (int j = 1; j <= days_in_week; ++j) {
// continue if the day is an even number
if (j % 2 == 0) {
continue;
}
printf (" Day:%d\n", j );
}
}
return 0;
}
Output
Week: 1 Day:1 Day:3 Day:5 Day:7 Week: 2 Day:1 Day:3 Day:5 Day:7 Week: 3 Day:1 Day:3 Day:5 Day:7
This program prints only those days that are even.
Whenever the days_in_week is even, the continue
statement skips that iteration of the inner loop.
We hope that this tutorial helped you develop better understanding of the concept of Nested Loops in C.
Keep Learning : )