For loops: How does c++ know when they end?

I'm working my way through "Beginning C++ Through Game Programming Second Edition" and on chapter 3, I'm introduced to "for" loops. The example code is as follows:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
// Counter
// Demonstrates for loops

#include <iostream>

using namespace std;

int main()
{
    cout << "Counting forward:\n";
    for (int i = 0; i < 10; ++i)
        cout << i << " ";

    cout << "\n\nCounting backward:\n";
    for (int i = 9; i >= 0; --i)
        cout << i << " ";

    cout << "\n\nCounting by fives:\n";
    for (int i = 0; i <= 50; i += 5)
        cout << i << " ";

    cout << "\n\nCounting with null statements:\n";
    int count = 0;
    for ( ; count < 10; )
    {
        cout << count << " ";
        ++count;
    }

    cout << "\n\nCounting with nested for loops:\n";
    const int ROWS = 5;
    const int COLUMNS = 3;
    for (int i = 0; i < ROWS; ++i)
    {
        for (int j = 0; j < COLUMNS; ++j)
            cout << i << "," << j << "  ";
        cout << endl;
    }

 return 0;
}


While I understand what the code does, I don't understand how c++ knows when the loop code is supposed to end. In Visual Basic, there was always something specifying the end of a loop. With "while" and "do" loops in c++ there are squiggle brackets enclosing the loop. To me, much of this looks like loops inside of loops. Is it because the other ones only have a single line of code following them that they don't have to be enclosed in squiggle brackets?
The condition is what controls the loop.
for (initialization ; condition ; update)
The idea is that in a for loop, the loop is controlled by a counter, so it will run a determinate, finite number of times. The counter is, traditionally, increased after every execution of the loop. Once the condition is false, the loop ends and the code moves on.
for (int i = 0; i < 7; i++)
That for loop would run 7 times. After running for i = 6, it would be incremented to seven and seven is not less than seven. So the loop would end. All loops in C++ are condition driven.
Also note the difference between a regular bracket and a curly brace. Curly brackets denote scope and code block. Normal brackets are for calling functions and storing conditions.
Read this:
http://www.cplusplus.com/doc/tutorial/control/
EDIT: By the way... those single line loops? Those are always allowed. If, after a control structure statement (exception being switch, I believe), you only have one statement, you can skip the curly brackets. That includes all loops and the if/else if/else structures.
1
2
while (true)
    cout << "Annoyed yet?"; // totally legal 

EDIT 2: (Wow this site is lagging today) Also denote the difference between a line and a statement.
cout << "hey"; cout << " this"; cout << " is"; cout << " bad"; cout << " style";
All that is five statements. You could cram the entirety of main onto one line and as long as you had the semicolons in their proper places, C++ doesn't care about the leading whitespace. This would even work, I think:
cout << "even";cout << " worse";cout << " style";
Last edited on
if, else, while, for, etc all have the same pattern. They execute the next statement (up to the next semicolon) or the next code block (the next set of {braces})

1
2
3
4
5
6
7
// this example has no braces
//  it will go to the next semicolon:

if(something)
  Foo();

Bar();  // this is not part of the above 'if'.  The 'if' stops at the semicolon after Foo() 


1
2
3
4
5
6
7
8
9
10
11
// this example has braces
//  it will go through the code block:

if(something)
{
  Foo();

  Bar();  // now, this IS part of the 'if' because it is inside the braces
}

Baz();  // but this is outside, so it's not part of the 'if' 


1
2
3
4
5
6
7
8
9
10
// this example is perfectly legal syntax and will compile okay, but
//  is probably a mistake

if(something);
{
  Foo();  // this is not part of the if!
}

// notice that there's a semicolon after the if.  Therefore the if stops immediately (it effectively has nothing in it)
//  therefore Foo() is not part of the if because the if has already ended 
Ah, I didn't know you could put multiple statements on the same line which is why I mentioned line instead of statement. I guess no one does that very often for the sake of clean code.

Question answered. Thank you very much.
Topic archived. No new replies allowed.