# Are for loops supposed to be read inward or outward? [closed]

`````` for (i = 0; i < 3; i++)
{
for (j = 0; j < 4; j++)
{
cout << arr2d[i][j] << "\t";
}
cout << endl;
.....
``````

Like that for example. Do you read the for loop inward-out (starting from `for (j = 0; j < 4; j++)`) or outward-in (starting from `for (i = 0; i < 3; i++)`) Just wondering. :D

You can't really read loops inward or outward, it's a bit more complex than that.

The loops start with the outer loop, but for each iteration in that loop, the inner loop goes through a complete cycle.

If you would print out the counters inside the inner loop, you would see:

`````` i   j
--- ---
0   0
0   1
0   2
0   3
1   0
1   1
1   2
1   3
2   0
2   1
2   2
2   3
``````
• So does that mean when "i" loops and finishes, "j" also finishes a loop? – ikabod197 Jan 11 '14 at 3:29
• @ikabod197: When the outer loop finishes, the inner loop has already finished the last time. The inner loop finishes once for each step in the outer loop. – Guffa Jan 11 '14 at 3:32
• Ohhh, ok, now I understand it better than before. Thansk for the help! :D – ikabod197 Jan 11 '14 at 3:38

This is a bit of a difficult question to answer. I would READ the loops from the top down. I'll assume then that your are referring to the order the code is executed in. When you see a loop structure, such as the one in your code, the outermost loop is the first thing that runs.

`````` for (i = 0; i < 3; i++) //This indicates that whatever is in between the braces that
//follow will be run 3 times.
{
for (j = 0; j < 4; j++)
{
cout << arr2d[i][j] << "\t";
}
cout << endl;
}
``````

The next line of code is what is run. In your case, that's another loop. Rinse and repeat.

`````` for (i = 0; i < 3; i++)
{
for (j = 0; j < 4; j++) //This loop will execute 4 times for each iteration
//of the outer loop
{
cout << arr2d[i][j] << "\t";   //Therefore, this statement is executed
//12 times in total
}
cout << endl; //and this one only 3, as it is outside of the inner loop.
}
``````

So how to read them? From the top down. Trace your braces, and remember that the first line of code that happens after the braces will be the one to run first. To put it in your words, "outward-in", just be sure you don't think that loops take a higher precedence than other code. If we were to insert a `cout` between the first and second loop, the outer loop would begin execution, then the `cout`, and finally the inner loop (and it's contents) followed by any statements after it.

I think you need to think in terms of building blocks, rather than reading in or out. First off add the trailing brace as follows:

``````for (i = 0; i < 3; i++)
{
for (j = 0; j < 4; j++)
{
cout << arr2d[i][j] << "\t";
}
cout << endl;
}
``````

``````cout << arr2d[i][j] << "\t";
``````

It's what will get executed again and again by the for statements.

The next block is this:

``````for (j = 0; j < 4; j++) { <something here> }
``````

This is the looping function. And the outer for block (at top) is similar. The two blocks together can also be written like this without the braces since it's just one statement that you need to loop on, which give you another way to look at it:

``````for (j = 0; j < 4; j++) cout << arr2d[i][j] << "\t";
``````

In earlier days programming languages like BASIC were not structured. A program consisted of numbered lines of code and you could jump from any line to any other line by using a GOTO statement. This made it very difficult to read a program as you could not understand it by reading the lines top-down. It was also very difficult to maintain such a code.

Then the so called Structured programming was introduced. The idea behind it is that the program consists of blocks that appear in a strict top-down order. Blocks can also be nested. A loop, for instance is, a block with nested blocks that can be executed repeatedly. These nested blocks are again executed in a strict top-down order.

C++ is a structured programming language. (There is a `goto` statement so, which should only be used in exceptional cases.) Therefore you can read a C++ routine in a top-down order.

Nested for-loops are just a special case of a loop-block containing a nested loop-block again. Your code consists of an outer loop-block having two nested blocks that can be executed repeatedly. It happens that the first of the two nested blocks is a loop-block again. The second nested block will be executed once the first has terminated, as with any other non-loop block (or statement). Once the last nested block has been executed, the control is passed to back to the outer loop-block which in turn "decides" whether the nested blocks will be executed again.