How is this code valid?

How is this code valid? I found it in a book of mine, and to me it seems like the author has missed out on a lot of {:s and }:s. But the whole program runs perfectly and I didn't get any errors.

1
2
3
4
5
6
for(int k=0;k<siff.size()-1;k++)
    if(siff[k]>siff[k+1])
        sum+=siff[k];
    else
        sum-=siff[k];
    cout << sum << endl;
Well, it's certainly legal C++. The entire "if... else..." block is considered to be a single statement, so there's no need for braces around it. You don't need braces around a block, if it contains a single statement. Although I, and many other developers, would argue that it is still good practice to have them anyway.

However, the way that cout statement is indented is misleading. It looks as though it is intended to execute in every loop iteration. But the absence of braces means that it will only execute once, after the loop has finished.

All of which ties in very nicely with the discussion in this thread:

http://www.cplusplus.com/forum/beginner/105019/
Last edited on
You don't need braces around a block, if it contains a single statement.

Yeah, I thought so as well but since there was one if-statement and one else-statement I figured that braces were needed.

However, the way that cout statement is indented is misleading. It looks as though it is intended to execute in every loop iteration. But the absence of braces means that it will only execute once, after the loop has finished.

Why isn't it part of the "collective" single line like the if- and else-statement when it is written like that?
Because it's a different statement.

Think of it like this:

The statement immediately after the "for" statement is an "if" statement. That statement isn't considered to be finished until the end of the final conditional block that it's comprised of.

For example, the following:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
if (a == 1)
{
  b = 2;
  c = 3;
  d = 4;
}
else if (a == 2)
{
  e = 5;
  f = 6;
  g = 7;
}
else
{
  h = 8;
  i = 9;
  j = 10;
}

is all one statement! It's a single "if... else" statement. It may be comprised of many sub-statements, but the whole thing is one big "if... else..." statement.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
if (a == 1)
{
  b = 2;
  c = 3;
  d = 4;
}
else if (a == 2)
{
  e = 5;
  f = 6;
  g = 7;
}
else
{
  h = 8;
  i = 9;
  j = 10;
}

So it would be legal to do:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
for(int k=0;k<siff.size()-1;k++)
  if (a == 1)
  {
    b = 2;
    c = 3;
    d = 4;
  }
  else if (a == 2)
  {
    e = 5;
    f = 6;
    g = 7;
  }
  else
  {
    h = 8;
    i = 9;
    j = 10;
  }

And, incidentally, that whole code snippet would be considered to be one single for statement, syntactically. Again, it's comprised of several different sub-statements, but it's also one single statement.

The "cout" statement, on the other hand, comes after the "if" statement has finished. It's a separate statement.

Having said all of that, there are many good reasons to put the braces in anyway (and few good reasons to leave them out).
Last edited on
If statements has one of the following forms:

if ( condition ) statement
if ( condition ) statement else statement

So the if and else parts are not separate statements.

for loops has the following form:

for ( for-init-statement conditionopt ; expressionopt ) statement

Note that statement in the if and for statement is only one statement. That's why we sometimes use curly brackets  to create a compound statement, which is a statement that can contain many statements.
Last edited on
I see. Thanks guys :)
Topic archived. No new replies allowed.