0

Consider:

for(int i = 10; b >= i; i++){
    if(i%2 == 0)
        cout << "even" << endl;
    else
        cout << "odd" << endl;
}

for(int i = 10; b >= i; i++){
    if(i%2 == 0){
        cout << "even" << endl;
    }else{
        cout << "odd" << endl;
    }
}

Both of these code work with the only difference being the curly brackets for the if else statement. When should I use curly brackets and when not?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
  • 1
    Please call them "curly braces" or just "braces". "curly brackets" is also acceptable (that is, after all, the root of their Unicode name). But not "curly arrow". – Wyck Oct 28 '20 at 04:01
  • 1
    [This question](https://stackoverflow.com/q/14901919/10957435) is about C, but it's practically the same best I can tell. I'm not confident in that enough though to close as a duplicate. –  Oct 28 '20 at 04:06
  • @Wyck I prefer the term "curvaceous pointy line" – TylerH Oct 28 '20 at 15:19

3 Answers3

3

They're called braces or curly brackets, not to be confused with the "curly arrow" in some languages, ~>.

In C, and by inheritance C++, these are optional on single-line if statements, but as many, many bugs have been created by omitting them you'd be advised to use them as a matter of principle even when they're redundant.

That is a mistake like this is easy to overlook:

if ((err = SSLHashSHA1.update(&hashCtx, &signedParams)) != 0)
    goto fail;
    goto fail;

Where it seems like that goto is conditional, yet it's not, it just drops through. This is the huge OpenSSL bug that caught everyone by surprise, and if veteran developers can mess it up, so can you.

The second form is the most reliable, least ambiguous, especially when formatted according to typical conventions:

for (int i = 10 ; b >= i;i++) {
    if (i%2 == 0) {
        cout << "even" << endl;
    }
    else {
        cout << "odd" << endl;
    }
}

for is a statement, not a function, so the syntax is for (...) with a space. Functions have no space, like f(...). Omitting the space implies for is a function, which it absolutely is not. The same goes for if, while and so on.

It's worth noting that the original code can actually be reduced to:

for (int i = 10 ; b >= i;i++)
    if (i%2 == 0)
        cout << "even" << endl;
    else
        cout << "odd" << endl;
    

Since that if is a single statement, even with the else clause attached.

Again, this is not advised because the rules of what is and isn't a single statement can be confusing.

tadman
  • 208,517
  • 23
  • 234
  • 262
  • For curiosity, what are some languages that uses the "curly arrow"? – Ranoiaetep Oct 28 '20 at 06:04
  • 1
    @Ranoiaetep Scala has an `~>` operator that's sometimes used for things like connecting streams. – tadman Oct 28 '20 at 06:45
  • That sounds like it wasn't Apple's fault. [It *was* Apple's fault](http://arstechnica.com/security/2014/02/extremely-critical-crypto-flaw-in-ios-may-also-affect-fully-patched-macs/) – Peter Mortensen May 27 '22 at 22:55
0

If the 'if', 'else' or 'for' structures have only one statement inside them, you can decide not to use the curly brackets. However, I would recommend to use them in order to improve the code readability.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
IrvinRamiA
  • 31
  • 2
  • 1
    Your terminology is just a bit off. Braces form _compound statements_. Where you said _instruction_ you should have said [_statement_](https://en.cppreference.com/w/cpp/language/statements). – Wyck Oct 28 '20 at 04:04
0

Google has a detailed C++ style guide that may help you. In particular, it says that

In general, curly braces are not required for single-line statements, but they are allowed if you like them; conditional or loop statements with complex conditions or statements may be more readable with curly braces. Some projects require that an if must always have an accompanying brace.

Jared
  • 1