Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the purpose of using braces (i.e. {}) for a single-line if or loop?

I'm reading some lecture notes of my C++ lecturer and he wrote the following:

  1. Use Indentation // OK
  2. Never rely on operator precedence - Always use parentheses // OK
  3. Always use a { } block - even for a single line // not OK, why ???
  4. Const object on left side of comparison // OK
  5. Use unsigned for variables that are >= 0 // nice trick
  6. Set Pointer to NULL after deletion - Double delete protection // not bad

The 3rd technique is not clear to me: what would I gain by placing one line in a { ... }?

For example, take this weird code:

int j = 0; for (int i = 0 ; i < 100 ; ++i) {     if (i % 2 == 0)     {         j++;     } } 

and replace it with:

int j = 0; for (int i = 0 ; i < 100 ; ++i)     if (i % 2 == 0)         j++; 

What's the benefit of using the 1st version?

like image 984
JAN Avatar asked Aug 30 '12 08:08

JAN


People also ask

Is it compulsory to put braces for single statement with for loop?

They are optional. That is just how it is. If you don't use braces to group multiple statements into one, then only the first statement following the for or if preamble is considered part of that construct.

What is the purpose of braces in C++?

When writing a function, or a class, or an if statement, or a loop, C++ uses an opening curly brace to begin the body of the function, class, if/else statement, or loop. Then you put all the normal statements and close it all with a matching closing curly brace.

What is the braces symbol used for?

Informal uses A person may use them as a fancy alternative to parentheses or in texting to make emoticons. One common informal use of braces is to clarify a statement that uses multiple sets of parentheses within other parentheses.

Can the bracket {} be used to enclose a single line of code?

Answer: While curly brackets are mainly used to group several lines of codes, it will still work without error if you used it for a single line. Some programmers prefer this method as a way of organizing codes to make it look clearer, especially in conditional statements.


1 Answers

Let's attempt to also modify i when we increment j:

int j = 0; for (int i = 0 ; i < 100 ; ++i)     if (i % 2 == 0)         j++;         i++; 

Oh no! Coming from Python, this looks ok, but in fact it isn't, as it's equivalent to:

int j = 0; for (int i = 0 ; i < 100 ; ++i)     if (i % 2 == 0)         j++; i++; 

Of course, this is a silly mistake, but one that even an experienced programmer could make.

Another very good reason is pointed out in ta.speot.is's answer.

A third one I can think of is nested if's:

if (cond1)    if (cond2)        doSomething(); 

Now, assume you now want to doSomethingElse() when cond1 is not met (new feature). So:

if (cond1)    if (cond2)        doSomething(); else    doSomethingElse(); 

which is obviously wrong, since the else associates with the inner if.


Edit: Since this is getting some attention, I'll clarify my view. The question I was answering is:

What's the benefit of using the 1st version?

Which I have described. There are some benefits. But, IMO, "always" rules don't always apply. So I don't wholly support

Always use a { } block - even for a single line // not OK, why ???

I'm not saying always use a {} block. If it's a simple enough condition & behavior, don't. If you suspect someone might come in later & change your code to add functionality, do.

like image 169
Luchian Grigore Avatar answered Sep 26 '22 08:09

Luchian Grigore