Nested Loops in C with Examples

Learn via video course
FREE
View all courses
C++ Course: Learn the Essentials
C++ Course: Learn the Essentials
by Prateek Narang
1000
5
Start Learning
C++ Course: Learn the Essentials
C++ Course: Learn the Essentials
by Prateek Narang
1000
5
Start Learning
Topics Covered

Nested Loop refers to a loop within a loop, as the name implies. Within a loop, there can be any number of loops. We're all familiar with looping conditions such as for, while, and do-while. To create nested loops, we can loop multiple types of loops within one other. Nested Loops are supported by the C programming language.

Syntax

The syntax of Nested Loop in c is shown below.

1. Nested For Loop

Syntax: The syntax for nested for loop is shown below.

Flow chart: The flow chart for the nested for loop is shown below.

Flowchart of Nested For Loop in C

Example: The example for a nested for loop is shown below.

An annual examination in m subjects is given to a class of n students. A program that takes each student's marks in various subjects and calculates and prints the overall grade for each of them.

Output:

Explanation:

Two for loops are used in the program, one to handle the number of students and the other to handle the number of subjects. Because the program asks for both the number of students and the number of subjects, it can be used for any size class with any number of subjects.

The outer loop is divided into three sections, as follows:

  • students' roll numbers are scanned out one by one;
  • the inner loop, where each student's marks are scanned and totaled; and
  • Total marks and division declarations are printed.

2. Nested While Loop

Syntax: The syntax for a nested while loop is shown below.

Flow chart: The flow chart for a nested while loop is shown below.

Flowchart of Nested While Loop in C

Example: The example for a nested while loop is shown below.

A program to print the 2-dimensional array using a while loop.

Output:

Explanation:

This program comprises two nested while loops. The variable row handles the outer loop. The variable cols handle the inner loop.

3. Nested Do-while Loop

Syntax: The syntax of a nested do-while loop is shown below.

Flow chart: The flow chart of a nested do-while loop is shown below.

Flowchart of Nested do-while Loop in C

Example: The example of a nested do-while loop is shown below.

A program to print the multiplication table from 1 x 1 to 10 x 10.

Output:

Explanation::

This program comprises two nested do...while loops. The variable row handles the outer loop, which is executed 10 times. The variable in the variable column handles the inner loop, which is run 10 times before the outer loop is run. That is, the inner loop is executed 100 times, printing a value in the table each time.

More Examples of Nested Loop in C

Example 1

A C Program to print solid, hollow patterns using nested for loop.

Output:

Example 2

Pyramid pattern of number using nested while loop.

Output:

Example 3

A C program using a nested do-while loop.

Output:

Example 4

A C Program to print pascal triangle using nested for loop.

Output:

Conclusion

  • A nested loop is a loop statement that is included within another loop statement.
  • All nested loops have been examined in terms of flow chart, syntax, and examples.
  • The simplest way to understand how a nested loop works are to solve pattern printing problems.