4

I have a program that uses for loops to check conditional statements and run tests on sets of data, and most of these are one liner if statements that could possibly branch off. My question is how does a for loop with brackets decide what is in and out of scope? For example:

A for loop in my program:

for( i =0; i < (sizeof(exact_roots) / sizeof(bn_comlplex)); i++){
        if(fabs(roots[k].re - exact_roots[i].re) < 0.00001 && fabs(roots[i].im - exact_roots[i].im) < 0.00001)
            rootmatch++;
}

are brackets needed in this case? Would the for loop treat the third line as apart of the for loop or discard it and give me a compile error?

what about an extreme case of a for loop with no brackets, how does the loop handle it?

for(i = 0; i < num; i++)
    if(something)
        ....
    else //is this still considered apart of the loop?
        ....
Paolo
  • 20,112
  • 21
  • 72
  • 113
Syntactic Fructose
  • 18,936
  • 23
  • 91
  • 177

4 Answers4

5
for(i = 0; i < num; i++)
    if(something)
        ....
    else //is this still considered apart of the loop?
        ....

Yes, it's still part of the loop. The entire if () .... else ... construct is one statement.

But using braces relieves you of the worry. With braces, it's immediately clear what the scope is.

Daniel Fischer
  • 181,706
  • 17
  • 308
  • 431
4

Only the next statement is part of the loop.

In your example the if ... else ...; is one statement.

Mark Byers
  • 811,555
  • 193
  • 1,581
  • 1,452
  • And also with braces - the point is that the braces and their content count as a single "compound statement" (or "block"). (+1, by the way) – Matteo Italia Dec 21 '12 at 18:43
  • so it would be safe to use no braces for the example loop as i posted above and for others? or should i consider keeping the braces – Syntactic Fructose Dec 21 '12 at 18:43
  • @Need4Sleep: in case of doubt, keep the braces to avoid surprises and make extra clear what you intend (it applies also to doubt about operators precedence). – Matteo Italia Dec 21 '12 at 18:44
  • @Need4Sleep: Personally I would recommend always using braces to avoid confusion and reduce the risk of bugs, but in the end it's a matter of taste. Some people prefer to omit the braces when it is allowed in order to save vertical space. There are [whole questions](http://stackoverflow.com/questions/1916576/mandatory-use-of-braces) devoted to this matter with opinions from both sides. – Mark Byers Dec 21 '12 at 18:44
  • It's really matter of code style. I recommend you to keep the braces, so others will have a better time understanding your code. – Andrejs Cainikovs Dec 21 '12 at 18:44
1

And we can get quite extreme:

for( i =0; i < (sizeof(exact_roots) / sizeof(bn_comlplex)); i++)
        if(fabs(roots[k].re - exact_roots[i].re) < 0.00001 && fabs(roots[i].im - exact_roots[i].im) < 0.00001)
            rootmatch++;
        else if (something)
           if (someother)
               do_stuff1();
           else if (third things)
                   do_stuff2()
                else
                   do_another();
        else
            do_failed();

Of course, if I codereview that, I wouldn't accept it unless it had braces, because quite soon you get confused about which if is inside which, and where you end up with the else.

I nearly always use braces around all if/while contents for that reason. Makes it easier to read the code, and if it's easy to read, it's easy to maintain.

And of course, adding the extra braces helps when you suddenly decide to add a "printf("x = %d\n", x); inside one of the if-statements, and then have to add extra braces to make sure you get the desired effect and no compiler error.

There is absolutely nothing wrong with surplus [within reason] braces. They don't add to the execution time, and only take a few bytes extra in the source code. Unless of course you post patches to the linux kernel mailing list, in which extra braces are cause for rejected code.

Mats Petersson
  • 126,704
  • 14
  • 140
  • 227
0

Without braces, you can only have one statement execute under the for, if, and else statements. Braces allow for you to include a compound grouping of statements and make your code MUCH more readable.