Nested For Loop

I have a piece of code that I need to analyze to determine its output just by looking at it. I ran it and know what it looks like. I'm just having trouble with understanding the for loops. On a piece of paper, I wrote two columns for the variables i and j and I don't what their values are during certain loops of the code.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
using namespace std;
int main()
{
    int i, j;

    for (i = 1; i <= 5; ++i)
    {
        for (j = 1; j <= i; ++j)
            cout << '@';
        for (j = i; j <= 5; ++j)
            cout << '#';
        cout << endl;
    }
    i = 9;
    while (i > 0)
    {
        i = i - 3;
        cout << i << endl;
    }
return 0;
}
Last edited on
Think logically about the two inner for loops - where does the first one end and where does the second one start?
Try adding statements to trace the values of i and j. Something like:

http://ideone.com/BQCMJl
He can't do that, he has to do it without running or compiling it.
@LB:
Sounds a wee bit too late for that:
"I ran it and know what it looks like"
He knows what the output is, but he doesn't know what the value are at certain points. I don't think it's "too late for that" ;)
Well the trouble that I'm having is that for the two inner 'for loops', when the first inner one is finished running, does it repeat the same loop or does it go to the second inner loop.
Well the trouble that I'm having is that for the two inner 'for loops', when the first inner one is finished running, does it repeat the same loop or does it go to the second inner loop.

Think of it this way:

1
2
        for (j = 1; j <= i; ++j)
            cout << '@';

is a statement.

1
2
        for (j = i; j <= 5; ++j)
            cout << '#';

is a statement.

The first statement is executed first, and the second statement is executed second :)
Last edited on
So is the first statement executed until it evaluates to false and then goes to the second one or does the first loop execute, then the second one and goes back to first one? I'm also confused on whether the values of i and j are reset to values in the first condition of the for loop....like for(j=1;...;...).....if j wasn't = 1, is it reset to a value of 1 when there is a pre-increment (++j)?
closed account (o3hC5Di1)
Hi there,

Let's clarify:

Say I have a For loop A, which contains loops B and C:

1
2
3
4
5
6
7
8
for A, run 3 times
{
    for B, run 2 times
    //some code

    for C, run 2 times
    //some code
}


B and C are a part of the code that A repeats. So every time A executes, B and C are executed entirely again.

The order would thus be:

A1
-B1
-B2
-C1
-C2

A2
-B1
-B2
-C1
-C2

A3
-B1
-B2
-C1
-C2



Hope that clarifies it for you. It's important to remember that a script is, generally, executed from top to bottom. So B is executed before C because it is written before C in the code.

All the best,
NwN
So is the first statement executed until it evaluates to false and then goes to the second one or does the first loop execute, then the second one and goes back to first one?


Why would it go back to the first one?

The first statement is a loop for j over values of 1 to i. It executes entirely.

Then, the second statement executes entirely. It is a loop for j over values of i to 5.

Statements don't magically interrupt each other. Each statement executes in turn. (*) It's not going to perform a bit of the first, then a bit of the second, then a bit of the first, etc.

It's all logical. One statement follows another.

I'm also confused on whether the values of i and j are reset to values in the first condition of the for loop....like for(j=1;...;...).....if j wasn't = 1, is it reset to a value of 1 when there is a pre-increment (++j)?

It sounds as though you don't understand quite what the syntax of a for loop means. The first part happens once, at the start of a loop. So, at the start of the first loop, j is set to 1. At the start of the second loop, j is set to i.

The third part happens at the end of each iteration of the loop, before the next check of the condition. It doesn't have anything to do with the initial condition of the loop.

So, at the start of the 1st iteration of the 1st loop, j (which is 1) is compared to i. If it's less than i, an iteration of the loop is performed. Then, at the end of that iteration, j is pre-incremented, so that it is now 2. It is then compared to i again. If it is still less than i, another iteration occurs, and then j is incremented again. And so on, until it fails the test of being less than i, at which point that loop stops.

Then the next loop begins. At the start, j is set equal to i. Then it is checked to see if it's less than 5. If so, a loop iteration is performed. At the end of the iteration, j is pre-incremented, and the condition is checked again.

Now, where in all this is the value of i changed? Look at the code, and tell me, logically, where you think the value of i changes.

(*) Well, not within a single thread, anyway. But I think threading is beyond the scope of this discussion :)
Last edited on
Thanks to all. MikeyBoy really cleared up things for me. I didn't understand the syntax of a for loop properly. So if I were to use a post-increment instead, then the value of the variable will increase by 1 after it's condition has been compared to a value?

Does the value of i increase by 1 every time the iteration of the second inner loop checks out false?
The third part of the for loop always happens last, whether you use pre increment or post increment.
Does the value of i increase by 1 every time the iteration of the second inner loop checks out false?

Not exactly. It is incremented at the end of each iteration of the outer loop. Which is not immediately the second inner loop exits, because there's a final cout << endl; within that iteration to execute first.

Within a single iteration of the outer loop - i.e. from lines 9-13 inclusive in your OP - the value of i stays the same. The only code that changes it is the ++i in the for statement, which happens at the end of each iteration, i.e. after each time lines 9-13 are executed.

The third part of the for loop always happens last, whether you use pre increment or post increment.
Yes - and it always happens before the loop condition is checked, regardless of whether you use post-increment or pre-increment.

Ignore the crossed out bit!
Last edited on
MikeyBoy wrote:
Yes - and it always happens before the loop condition is checked
This is not true for the first iteration, so this statement may be confusing.
Oops, yes, you're right. It does NOT happen before the first iteration happens. My apologies - what I wrote was misleading.

The order is always:

1) Perform the initial action given in the first part of the loop statement (e.g. j = 1).

2) Check the condition in the second part of the loop statement (e.g. j <= i). If it is false, exit the loop

3) Perform the statements inside the loop block

4) Perform the action given in the third part of the loop statement (e.g. ++j).

5) Go to 2)
Last edited on
Topic archived. No new replies allowed.