Nested Loop in C

Last Updated :

Prerequisite – Loops in C
A nested loop is a loop inside another loop. In C programming language, you can nest a loop inside another loop to perform repetitive tasks. The inner loop executes its entire cycle each time the outer loop executes one cycle.

Here is an example of a nested loop in C that prints a multiplication table:

for (int i = 1; i <= 10; i++) {     // outer loop for rows
    for (int j = 1; j <= 10; j++) { // inner loop for columns
        printf("%d ", i * j);
    }
    printf("\n");                   // print a newline after each row
}

In the above code, the outer loop iterates 10 times, once for each row in the multiplication table, and the inner loop iterates 10 times for each column in each row. The printf statement inside the inner loop prints the product of the row and column. After each row is printed, a newline character is printed to start a new line for the next row.

You can also have multiple nested loops in C to perform more complex tasks. Here is an example of a three-level nested loop:

for (int i = 1; i <= 3; i++) {           // outer loop for level 1
    for (int j = 1; j <= 3; j++) {       // inner loop for level 2
        for (int k = 1; k <= 3; k++) {   // innermost loop for level 3
            printf("%d %d %d\n", i, j, k);
        }
    }
}

In the above code, the outer loop iterates 3 times, once for each level-1 task. The inner loop iterates 3 times for each level-2 task, and the innermost loop iterates 3 times for each level-3 task. The printf statement inside the innermost loop prints the value of each loop variable for each task.

Please write comments below if you find anything incorrect, or you want to share more information about the topic discussed above. A gentle request to share this topic on your social media profile.

Comment
Next Article
Mithlesh Upadhyay Published 21 Feb, 2023 · 2 min read

Mithlesh Upadhyay is a Computer Science and AI expert from Madhya Pradesh with strong academic background (BE in CSE and M.Tech in AI) and over six years of experience in technical content development. He has contributed tech articles, led teams, and worked in Full Stack Development and Data Science. He founded the w3colleges.org portal for learning resources.

Article Tags :

Similar Reads

  • What should we use void main() or int main() ?

    There is question that what should we use void main() or int main() ? void main() { /* ... */ } Or, int main() { /* ... */…

    2 min read
  • C Comments

    Prerequisite – C Language Introduction C comments explain code and improve readability. These do not affect program execution. You can use comments in C to clarify code and describe…

    1 min read
  • C Programming Language Standard

    Prerequisite – C Language Introduction The C programming language has various versions: C89/C90, C99, C11, and C18. C89/C90: Released in 1989/1990. It introduced key language features. C99: This…

    2 min read
  • Features of C Programming Language

    Prerequisite – C Language Introduction C is a simple language made in 1972 by Dennis Ritchie. It’s for system programming. C has low-level memory access, basic keywords, good…

    1 min read
  • C Language Introduction

    C is a programming language developed by Dennis Ritchie in 1972. It has a significant historical impact. It was originally created at Bell Laboratories of AT&T Labs for…

    4 min read
  • Code editors

    Code editors are where programmers spend most of their time. There are two main types of code editors: IDEs and Lightweight editors. IDEs (Integrated Development Environments) IDEs (Integrated…

    1 min read