C Nested Loop

In this tutorial you will learn about the C Nested Loop and its application with practical example.

C Nested Loop

In c programming, loop within a loop is called a nested loop. In this tutorial, we will learn about nested loops in c programming with examples.

What Is Nested Loop

When there is a loop statement inside another loop statement then it is known as a nested loop statement. It is possible to have a loop statement as a part of another loop statement in c programming. There can be any number of loops inside a loop. It is not necessary that the loop must be nested inside the same type of loop. There can be any type of loop nested inside the body of another loop, such as a while loop or for a loop. For example, we can have an inner for loop inside the outer while loop.

Syntax:-

How nested loop works

For every pass of the outer loop triggers the inner loop. This repeats until the outer loop completes all iterations. For each iteration of the outer loop inner loop execute all its iteration. It is possible to have a break statement within either the inner or outer loop to interrupt this process. A total number of iterations is equal to the number of iterations of the outer loop multiplied by the number of iterations in an inner loop.

Nested for Loop

When there is for loop inside another for loop statement then it is known as a nested for loop statement. It is possible to have a for loop statement as a part of another for loop statement. Below is the general syntax of nested for loop statements in c programming:

Syntax:-

Below is a simple example to demonstrate the use of nested for loop statement in c programming. In this example, we are using a for loop inside a for a loop.

Example:-

Output:-

c-nested-for-loop-example

Nested while Loop

When there is a while loop inside another while loop statement then it is known as a nested while loop statement. It is possible to have a while loop statement as a part of another while loop statement. Below is the general syntax of nested while loop statement in c programming:

Syntax:-

Below is a simple example to demonstrate the use of a nested while loop statement in c programming:

Example:-

Output:-

c-nested-while-loop-example

Nested do-while Loop

When there is a do-while loop inside another do while loop statement then it is known as a nested do-while loop statement. It is possible to have a do-while loop statement as a part of another do while loop statement. Below is the general syntax of nested do…while loop statement in c programming:

Syntax:-

Below is a simple example to demonstrate the use of a nested do-while loop statement in c programming:

Example:-

Output:-

c-nested-do-while-loop-example

When To Use a Nested Loop

Nested loops are handy when you want to loop through multi-dimensional data. For example, it is easy to traverse a multi-dimensional array using nested loops.

Break In Nested loop

The break statement is used to exit out of the loop. If the break statement is used inside a nested loop, it will terminate the innermost loop.

Continue In the Nested loop

The continue statement is used to skip the current iteration and move to the next iteration. When a continue statement is encountered inside a loop, it skips all the statements below it and jumps the control to the next iteration.

In this tutorial we have learn about the C Nested Loop and its application with practical example. I hope you will like this tutorial.