-5

While practising nested loops in C, I wanted to understand how control flows between an outer loop and an inner loop. I understood that the inner loop runs for each iteration of the outer loop, but the order of execution still confuses me.

Example code:

#include <stdio.h>

int main(void)
{
    for (int i = 0; i < 2; i++) {          /* outer loop: i = 0..1 */
        for (int j = 0; j < 3; j++) {      /* inner loop: j = 0..2 */
            printf("i = %d, j = %d\n", i, j);
        }
    }
    return 0;
}

Output produced:

i = 0, j = 0
i = 0, j = 1
i = 0, j = 2
i = 1, j = 0
i = 1, j = 1
i = 1, j = 2

I initially thought i would increment once every time j increments, but it appears that the inner loop runs completely for each value of i. Why does the inner loop execute all its iterations before i increments? How should I think about the execution path in nested loops in C? Does this behaviour differ in other programming languages?

I have traced the values of i and j using printf statements and also stepped through the code with a debugger. I still find the order counter‑intuitive and would appreciate a deeper explanation or any visualisation techniques to understand it better.

2
  • 2
    I understood that the inner loop runs for each iteration of the outer loop and inner loop runs completely for each value of i, what exactly you understood? Commented Sep 6 at 13:20
  • A loop statement is a statement like any other statement. A statement is executed the same way whether if it is inside a(nother) loop or not. j++ reads "increment j". It does not read "increment j and also at the same time increment i and oh I don't know what else, look it up, maybe there's another loop out there". Why would one find the first reading counter-intuitive and the second one not? I'm all confused. Commented Sep 6 at 19:31

3 Answers 3

2
int main(void)
{
    for (int i = 0; i < 2; i++) 
    {          
        for (int j = 0; j < 3; j++) 
        {     
            printf("i = %d, j = %d\n", i, j);
        }
    }
    return 0;
}

How should I think about the execution path in nested loops in C? Does this behaviour differ in other programming languages?

No, it is exactly as any other programming language. Loops are not related. The nested loop is executed during every iteration of the outer loop.

In C, a for loop is just a statement. Your inner for is a single statement inside the body of the outer for.

Sign up to request clarification or add additional context in comments.

Comments

2

The order of execution of the clauses of the for loops may seem confusing to C beginners. Consider this conversion to while loops:

#include <stdio.h>

int main(void)
{
    int i = 0;
    while (i < 2) {          /* outer loop: i = 0..1 */
        int j = 0;
        while (j < 3) {      /* inner loop: j = 0..2 */
            printf("i = %d, j = %d\n", i, j);
            j++;
        }
        i++;
    }
    return 0;
}

Here the execution order is simpler to understand: j++ is performed after each call to printf and the inner loop test is performed 4 times, the last of which evaluating to false, causing control to pass to the i++ statement.

The only difference between this conversion and the for statement is the behavior of the continue statement that transferts control to the increment clause in the for loop compared to the test in the while loop.

Comments

1

The relevant rules in C are:

  1. Each statement in C is fully executed before program control passes to the next statement.1, 2

  2. Fully executing this code:

        for (int j = 0; j < 3; j++) {      /* inner loop: j = 0..2 */
            printf("i = %d, j = %d\n", i, j);
        }

executes the printf statement three times. During execution of this single for loop, there is no interaction with any statement outside this.

  1. This for loop is a substatement of another for loop, for (int i = 0; i < 2; i++).3 The specification of for says its substatement is executed repeatedly, subject to the control and update expressions of the for statement.

Therefore, to execute the outer for loop, i is set to 0, and the inner for loop is executed completely. Then i is incremented to 1, and the inner for loop is executed completely. Then i is incremented to 2, and the outer for loop completes.

Footnote

1 Fully executing a statement includes executing statements contained within it. The statements within in are not the “next“ statement.

2 While a statement is completed before control passes to the next statement, it may have effects that are not immediately visible and may appear after later statements. For example, the output that a statement sends to standard output might be buffered inside the program and not appear in a terminal window until later.

3 The inner for loop is actually a substatement of a {} statement, called a compound statement, and this compound statement is the direct substatement of the outer for loop. Effectively, the inner for loop is the whole body of the outer for loop.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.