Nested Loops in C Programming
Course Instructor:
Dr. Suman Kumar Maji
IIT Patna Chapter-4(1) 1 / 26
Nested Loops
Nesting of loops is the feature in C that allows the looping of
statements inside another loop.
Any number of loops can be defined inside another loop, i.e., there
is no restriction for defining any number of loops.
The nesting level can be defined at n times.
You can define any type of loop inside another loop.
For example, you can define ’while’ loop inside a ’for’ loop.
IIT Patna Chapter-4(1) 2 / 26
Nested Loops
Syntax
1 Outer_loop
2 {
3 Inner_loop
4 {
5 // inner loop statements .
6 }
7 // outer loop statements .
8 }
Outer loop and Inner loop are the valid loops that can be a ‘for’
loop, ‘while’ loop or ‘do-while’ loop.
IIT Patna Chapter-4(1) 3 / 26
Nested For Loop
The nested for loop means any type of loop which is defined inside
the ‘for’ loop.
Syntax
1 for ( initia lizatio n ; condition ; update )
2 {
3 for ( in itializa tion ; condition ; update )
4 {
5 // inner loop statements .
6 }
7 // outer loop statements .
8 }
IIT Patna Chapter-4(1) 4 / 26
Nested For Loop
Example
1 # include < stdio .h >
2
3 int main () {
4 int i , j ;
5
6 // Outer loop
7 for ( i = 1; i <= 2; ++ i ) {
8 printf ( " Outer : % d \ n " , i ) ; // Executes 2 times
9
10 // Inner loop
11 for ( j = 1; j <= 3; ++ j ) {
12 printf ( " Inner : % d \ n " , j ) ; // Executes 6 times (2 * 3)
13 }
14 }
15
16 return 0;
17 }
IIT Patna Chapter-4(1) 5 / 26
Nested For Loop
Output
1 Outer : 1
2 Inner : 1
3 Inner : 2
4 Inner : 3
5 Outer : 2
6 Inner : 1
7 Inner : 2
8 Inner : 3
IIT Patna Chapter-4(1) 6 / 26
Nested For Loop
Explanation
First, the ‘i’ variable is initialized to 1 and then program control
passes to the i <= 2.
The program control checks whether the condition ‘i <= 2’ is true
or not.
If the condition is true, then the program control executes line 8 and
passes to the inner loop.
The inner loop will get executed until the condition is true.
After the execution of the inner loop, the control moves back to the
update of the outer loop, i.e., i + +.
After incrementing the value of the loop counter, the condition is
checked again, i.e., i <= 2.
If the condition is true, then the inner loop will be executed again.
This process will continue until the condition of the outer loop is
true.
IIT Patna Chapter-4(1) 7 / 26
Nested While Loop
The nested while loop means any type of loop which is defined
inside the ‘while’ loop.
Syntax
1 while ( condition )
2 {
3 while ( condition )
4 {
5 // inner loop statements .
6 }
7 // outer loop statements .
8 }
IIT Patna Chapter-4(1) 8 / 26
Nested While Loop
Example
1 # include < stdio .h >
2
3 int main ()
4 {
5 int a = 1 , b = 1;
6
7 while ( a <= 5)
8 {
9 b = 1;
10 while ( b <= 5)
11 {
12 printf ( " % d " , b ) ;
13 b ++;
14 }
15
16 printf ( " \ n " ) ;
17 a ++;
18 }
19
20 return 0;
21 }
IIT Patna Chapter-4(1) 9 / 26
Nested While Loop
Output
1 1 2 3 4 5
2 1 2 3 4 5
3 1 2 3 4 5
4 1 2 3 4 5
5 1 2 3 4 5
IIT Patna Chapter-4(1) 10 / 26
Nested While Loop
Explanation
The program initializes the ‘a’ and ‘b’ variables by 1.
Now, control moves to the while loop, and this loop checks whether
the condition is true, then the program control reinitialized ‘b’
variable by 1 and moves to the inner loop.
After the execution of the inner loop, the control executes line 16
and moves to the update of the outer loop, i.e., a + +.
After incrementing the value of ‘a’, the condition (a <= 5) is
checked.
If the condition is true, the control then again moves to the inner
loop.
This process continues until the condition of the outer loop is true.
IIT Patna Chapter-4(1) 11 / 26
Nested Do-While Loop
The nested do..while loop means any type of loop which is defined
inside the ‘do..while’ loop.
Syntax
1 do
2 {
3 do
4 {
5 // inner loop statements .
6 } while ( condition ) ;
7 // outer loop statements .
8 } while ( condition ) ;
IIT Patna Chapter-4(1) 12 / 26
Nested Do-While Loop
Example
1 # include < stdio .h >
2
3 int main () {
4 int i = 1;
5 int j ;
6
7 do {
8 j = 1;
9 do {
10 printf ( " % d * % d = % d \ n " , i , j , i * j ) ;
11 j ++;
12 } while ( j <= 10) ;
13 i ++;
14 } while ( i <= 2) ;
15
16 return 0;
17 }
IIT Patna Chapter-4(1) 13 / 26
Nested Do-While Loop
Output
1 1 * 1 = 1
2 1 * 2 = 2
3 1 * 3 = 3
4 1 * 4 = 4
5 1 * 5 = 5
6 1 * 6 = 6
7 1 * 7 = 7
8 1 * 8 = 8
9 1 * 9 = 9
10 1 * 10 = 10
11 2 * 1 = 2
12 2 * 2 = 4
13 2 * 3 = 6
14 2 * 4 = 8
15 2 * 5 = 10
16 2 * 6 = 12
17 2 * 7 = 14
18 2 * 8 = 16
19 2 * 9 = 18
20 2 * 10 = 20
IIT Patna Chapter-4(1) 14 / 26
Nested Do-While Loop
Explanation
First, we initialize the outer loop counter variable, i.e., ‘i’ by 1.
As we know that the do..while loop executes once without checking
the condition, so the inner loop is executed without checking the
condition in the outer loop.
After the execution of the inner loop, the control moves to the
update of the i + +.
When the loop counter value is incremented, the condition is
checked. If the condition in the outer loop is true, then the inner
loop is executed.
This process will continue until the condition in the outer loop is
true.
IIT Patna Chapter-4(1) 15 / 26
Mixed Nested Loop
There is no rule that a loop must be nested inside its own type. In
fact, there can be any type of loop nested inside any type and to any
level.
Syntax
1 do {
2
3 while ( condition ) {
4
5 for ( in itializ ation ; condition ; increment ) {
6
7 // statement of inside for loop
8 }
9
10 // statement of inside while loop
11 }
12
13 // statement of outer do - while loop
14 } while ( condition ) ;
IIT Patna Chapter-4(1) 16 / 26
Types of Jump Statements in C
There are 4 types of jump statements in C language:
break
continue
goto
return
IIT Patna Chapter-4(1) 17 / 26
Break Statement
The break statement can be used to jump out of a loop.
Example jumps out of the for loop when i is equal to 4
1 # include < stdio .h >
2
3 int main () {
4 int i ;
5
6 for ( i = 0; i < 10; i ++) {
7 if ( i == 4) {
8 break ;
9 }
10 printf ( " % d " , i ) ;
11 }
12
13 return 0;
14 }
Output
1 0 1 2 3
IIT Patna Chapter-4(1) 18 / 26
Break Statement
Use of break statement in while loops
1 # include < stdio .h >
2
3 int main () {
4 int i = 0;
5
6 while ( i < 10) {
7 if ( i == 4) {
8 break ;
9 }
10 printf ( " % d " , i ) ;
11 i ++;
12 }
13
14 return 0;
15 }
Output
1 0 1 2 3
IIT Patna Chapter-4(1) 19 / 26
Continue Statement
The continue statement breaks one iteration (in the loop), if a
specified condition occurs, and continues with the next iteration in
the loop.
Example skips the value of 4
1 # include < stdio .h >
2
3 int main () {
4 int i ;
5
6 for ( i = 0; i < 10; i ++) {
7 if ( i == 4) {
8 continue ;
9 }
10 printf ( " % d " , i ) ;
11 }
12
13 return 0;
14 }
Output
1 0 1 2 3 5 6 7 8 9
IIT Patna Chapter-4(1) 20 / 26
Continue Statement
Use of continue statement in while loops
1 # include < stdio .h >
2
3 int main () {
4 int i = 0;
5
6 while ( i < 10) {
7 if ( i == 4) {
8 i ++;
9 continue ;
10 }
11 printf ( " % d " , i ) ;
12 i ++;
13 }
14
15 return 0;
16 }
Output
1 0 1 2 3 5 6 7 8 9
IIT Patna Chapter-4(1) 21 / 26
Goto Statement
Explanation
The goto statement is used to jump to a certain location within a
function from any other location.
IIT Patna Chapter-4(1) 22 / 26
Goto Statement – Example
1 # include < stdio .h >
2 int main () {
3 int choice ;
4 printf ( " Select an option :\ n " ) ;
5 printf ( " 1. Print ' Hello '\ n " ) ;
6 printf ( " 2. Print ' World '\ n " ) ;
7 printf ( " 3. Exit \ n " ) ;
8 scanf ( " % d " , & choice ) ;
9 switch ( choice ) {
10 case 1:
11 printf ( " Hello \ n " ) ; break ;
12 case 2:
13 printf ( " World \ n " ) ; break ;
14 case 3:
15 printf ( " Exiting ...\ n " ) ;
16 goto end ; // Jump to the ' end ' label to exit
17 default :
18 printf ( " Invalid choice \ n " ) ; break ;
19 }
20 // This is where the ' end ' label is defined
21 end :
22 printf ( " Program ends here .\ n " ) ;
23 return 0;
24 }
IIT Patna Chapter-4(1) 23 / 26
Goto Statement – Example
Output
1 Select an option :
2 1. Print ' Hello '
3 2. Print ' World '
4 3. Exit
5 3
6 Exiting ...
7 Program ends here .
IIT Patna Chapter-4(1) 24 / 26
Return Statement
When a function is finished running, the return statement in C is
used to give the caller a value.
It is often used to return a result to the caller code.
Example
1 # include < stdio .h >
2
3 int multiply ( int a , int b ) {
4 return a * b ;
5 }
6
7 int main () {
8 int result = multiply (5 , 7) ;
9 printf ( " The product is : % d \ n " , result ) ;
10 return 0;
11 }
Output
1 The product is : 35
IIT Patna Chapter-4(1) 25 / 26
Thank you
IIT Patna Chapter-4(1) 26 / 26