EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 600+ Courses All in One Bundle
  • Login

Nested Loop in C

Home » Software Development » Software Development Tutorials » C Programming Tutorial » Nested Loop in C

nested loop in c

Introduction to Nested Loop in C

As the name already suggests, a loop inside a loop is called Nested Loop. There can be any number of loops inside a loop. We know there are generally many looping conditions like for, while, and do-while. We can loop different kinds of loops within each other to form nested loops. C language supports this functionality of Nested Loops. below is the syntax of Nested Loop in C.

Syntax:

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

Outside_loop
{
//Outside Loop Statements
Inside_loop
{
//Inside loop Statements
}
}

The above syntax is a single loop condition inside a loop condition. In this way, there can be many conditions too.

Outside_loop
{
//Outside Loop Statements
Inside_loop_1
{
//Inside loop 1 Statements
}
Inside_loop_2
{
//Inside loop 2 statements
}
Inside_loop_3
{
//Inside loop 3 statements
}
……… continues
}

Flowchart of Nested Loop

Here, let us see the actual process flow in case of these nested loops.

Nested Loop Flowchart

In the above flow chart, we can see that there are two conditions that are given. The inner loop condition gets executed only when the outer loop condition gives the Boolean output as True. Else the flow control directly goes out of both the loops. Now coming into the execution of the inner loop, If the loop condition gives a true result, then the block of statements under that loop and the incremental condition gets executed. And in turn, if the condition gives a Boolean condition as False, then the inner loop gives its control back to the outer loop, and again same conditions/loops gets executed/repeated.

Popular Course in this category
C Programming Training (3 Courses, 5 Project)3 Online Courses | 5 Hands-on Projects | 34+ Hours | Verifiable Certificate of Completion | Lifetime Access
4.5 (5,570 ratings)
Course Price

View Course

Related Courses
C++ Training (4 Courses, 5 Projects, 4 Quizzes)Java Training (40 Courses, 29 Projects, 4 Quizzes)

Examples to Implement Nested Loop in C

Let us see below few examples on the functionality of nested for loops in C and understand how it works through programs.

Example #1

Nested loop in ‘for’ condition. This we can generally use for creating or printing a multi-dimensional array.

Code:

#include <stdio.h>
int main()
{
int i,j,x,y;
int a[10][10];
printf("Enter value for x(rows)- max of 10: ");
scanf("%d", &x);
printf("Enter value for y(columns) - max of 10: ");
scanf("%d",&y);
printf("Let's create a 2-D array: ");
for(i=0;i<x;i++)
{
for(j=0;j<y;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("Now printing the array: ");
printf("\n");
for(i=0;i<x;i++)
{
for(j=0;j<y;j++)
{
printf("\t");
printf("%d",a[i][j]);
}
printf("\n");
}
return 0;
}

Output:

Nested Loop in C Example 1

Let us see how the above example code works:

  • Firstly, we declare the integer values for defining the number of rows and columns.
  • Next, the array declaration is done.
  • We then have to take the inputs from the user as per the values specified for the number of rows and columns.
  • The user input is taken with the help of ‘two for loops’ as we are creating a 2-D array.
  • The first ‘for-loop’ is for the number of rows and the second loop is for the columns.
  • In taking a user input for an array, we are considering it as a row by row concept.
  • So, when all the columns in the first row is completely filled, the compiler point would then increment come to the next row by which all the columns are filled, and the process continues.
  • The same process flow continues for the loops for printing the respective output in an array format.

In this manner, the nested loops are implemented. Now, let us have another example for nested loops.

Example #2

Code:

#include <stdio.h>
int main()
{
int x,y;
int k=1;
printf("Enter the number of rows: ");
scanf("%d", &x);
printf("Enter the number of columns: ");
scanf("%d", &y);
int a[x][y];
int i=1;
while(i<=x)
{
int j=1;
while(j<=y)
{
printf("%d\t",k);
k++;
j++;
}
i++;
printf("\n");
}
}

As seen above, we had created another 2-D array using “while loop”.

Output:

while loop Example 2

The same level compilation as to the ‘for loop’ is being done. Once the outer while loop gets a Boolean “True” as the output, the next compilation code goes into the inner condition. Once the inner condition gives the output as “False”, then the assignment again reaches to the outer loop condition.

Example #3

Here, we will have small inter mixture of for loops program.

Code:

#include <stdio.h>
int main()
{
int n=1;
int i;
while(n<5)
{
printf("*");
printf("\n");
n=n+1;
for(i=1;i<n;i++)
{
printf("$");
}
}
}

Output:

Nested Loop in C Example 3

In the above program, as you have noticed, we had printed two different symbols one after the other using while and for loop together. The combination of using different nested loops plays an important role in writing different level programs.

Example #4

Let us even look into an example dealing with the do-while nested loop. This example also lets print some random pattern.

Code:

#include <stdio.h>
int main()
{
int n=1;
int i=0;
do
{
printf("$");
printf("\n");
n=n+1;
do
{
printf("*");
i=i+1;
}while(i<n);
}while(n<5);
}

Output:

do-while Example 5

In the above program also, we have used nested do-while loops to print a pattern based on the given inputs.

NOTE: As an exercise, try possibilities in many ways of handling different loops together.

Conclusion

Here, we had got the basic syntax and got to understand a few examples with respect to different nested functions. We had learned how actually there would be the process flow through flow chart and explained the working of a nested ‘for’ loop.  So, keep practicing and enjoy learning C.

Recommended Articles

This is a guide to Nested Loop in C. Here we discuss the Introduction to Nested Loop in C and its examples along with the flowchart of nested loop. You can also go through our other suggested articles to learn more –

  1. Nested Loop in JavaScript
  2. Nested Loop in C++
  3. Nested Loop in Java
  4. Nested Loop in Matlab

C Programming Training (3 Courses, 5 Project)

3 Online Courses

5 Hands-on Projects

34+ Hours

Verifiable Certificate of Completion

Lifetime Access

Learn More

0 Shares
Share
Tweet
Share
Primary Sidebar
C Programming Tutorial
  • Loops
    • Loops in C
    • For Loop in C
    • While Loop in C
    • Do While Loop in C
    • Nested Loop in C
    • Infinite Loop in C 
  • Basic
    • Introduction to C
    • What is C
    • Career in C Programming
    • Advantages of C
    • How to Install C
    • Best C Compilers
    • Data Types in C
    • Variables in C
    • C Keywords
    • C Command
    • Command Line Arguments in C
    • C Literals
    • Constants in C
    • Unsigned Int in C
    • String in C
  • Pointers
    • Pointers in C
    • Null pointer in C
    • Function Pointer in C
    • Double Pointer in C
    • Void Pointer in C
    • Const Pointer in C
    • Dangling Pointers in C
    • Pointer Arithmetic in C
  • Operators
    • C Operators
    • Arithmetic Operators in C
    • Relational Operators in C
    • Assignment Operators in C
    • Logical Operators in C
    • Conditional Operator in C
    • Modulus Operator in C
    • Ternary Operator in C
    • Address Operator in C
    • Unary Operator in C
    • Operators Precedence in C
    • Left Shift Operator in C
  • Control Statement
    • Control Statements in C
    • If Statement in C
    • If-else Statement in C
    • Else if Statement in C
    • Nested if Statement in C
    • #else in C
    • Structure Padding in C
    • Nested Structure in C
    • Continue Statement in C
    • Break Statement in C
    • Switch Statement in C
    • Goto Statement in C
  • Function
    • Math Functions in C
    • Hashing Function in C
    • Recursive Function in C
    • Power Function in C
    • fputs in C
    • C puts() Function
    • fprintf() in C
    • fseek() in C
    • Stderr in C
    • ASCII Value in C
    • strcat() in C
    • Inline Function in C
    • sizeof() in C
    • Function Prototype in C
    • C ftell()
  • Array
    • Arrays in C Programming
    • 2-D Arrays in C
    • 3D Arrays in C
    • Multidimensional Array in C
    • Array Functions in C
    • Strings Array in C
  • Sorting
    • Sorting in C
    • Heap Sort in C
  • Advanced
    • Constructor in C
    • Encapsulation in C
    • C Storage Classes
    • Static Keyword in C
    • File Handling in C
    • Queue in C
    • Hexadecimal in C 
    • typedef in C
    • Memory Allocation in C
    • Linked List in C
    • Volatile in C
    • Tokens in C
    • Expression in C
    • Regular Expression in C
    • Error Handling in C
    • Types of Errors in C
    • Preprocessor in C
    • Preprocessor Directives in C
    • fscanf() in C
    • #Pragma in C
    • #ifndef in C
    • #undef in C
    • Macros in C
  • C programs
    • Patterns in C Programming
    • Star Patterns in C
    • Number Patterns in C
    • Swapping in C
    • Reverse Number in C
    • Palindrome in C Program
    • Factorial in C
    • Fibonacci Series in C
    • Square Root in C
    • Random Number Generator in C
    • Prime Numbers in C
    • Escape Sequence in C
    • Reverse String in C
    • Leap Year Program in C
    • Anagram Program in C
    • Strong Number in C
    • String Concatenation in C
    • C Programming Matrix Multiplication
    • Decimal to Octal in C
    • Expression Evaluation in C
    • Decimal to Hexadecimal in C
  • Interview question
    • C Programming Interview Questions

Related Courses

C Programming Training Course

C++ Training Course

Java Training Course

Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Java Tutorials
  • Python Tutorials
  • All Tutorials
Certification Courses
  • All Courses
  • Software Development Course - All in One Bundle
  • Become a Python Developer
  • Java Course
  • Become a Selenium Automation Tester
  • Become an IoT Developer
  • ASP.NET Course
  • VB.NET Course
  • PHP Course

© 2020 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you
Book Your One Instructor : One Learner Free Class

Let’s Get Started

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA Login

Forgot Password?

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you

Special Offer - C Programming Training (3 Courses, 5 Project) Learn More