Convince a colleague that they were not nested

One of my university colleagues who started programming this year sometimes writes if such statements look like this:

if(something) doA(); else if(something2) doC(); else doD(); 

He is convinced that the second pair of if-else considered as a single entity and that it is actually nested under the first else .

I am sure, however, that its code is equivalent:

 if(something) doA(); else if(something2) doC(); else doD(); 

This shows that the second other is not nested, but at the same level as the first if. I told him that he needed to use braces to achieve what he wants.

"But my code works as intended!"

And indeed, he worked as intended. It turns out that the behavior of the code was the same, even if else not nested.

Surprisingly, I found that I could not write a clear and concise example demonstrating different behavior between

 if(something) doA(); else if(something2) doC(); else doD(); 

and

 if(something) doA(); else { if(something2) doC(); else doD(); } 

Can you help me find an example that will show my colleague the difference between using / not using curly braces?

Or a mis-looking version, always equivalent to the one that has curly braces, in terms of behavior?

+7
c ++ c if-statement nested
source share
2 answers

Per C 2011 6.8.4 1, the grammar for the sentence of choice includes these products:

selection-statement: if ( expression ) else statement

Per 6.8 1, statement for statement:

statement: statement-statement

So in:

 if(something) doA(); else if(something2) doC(); else doD(); 

indented , if else form the select statement, which is the statement that appears in the else clause of the previous select statement.

The works that I showed show that this is a possible interpretation in the grammar of C. To make sure that this is the only interpretation, we notice that the text in the else clause of the original select statement must be an expression, because in the grammar of C there is no other product that creates else . (This is most easily seen by searching for the grammar in section A.2. Due to its size, I will not reproduce it here.) Thus, we know that else follows the instruction. We can easily see that the operator is a sentence of choice, since it starts with if . Then the only question remains is whether the next else is part of this if statement or not. Per 6.8.4.1 3, " else is bound to the lexically closest previous , if allowed syntax."

+11
source share

Both structures come to the same thing. The compiler effectively sees the code as:

 if ( something ) { doA() } else { if ( something2 ) { doC(); } else { doD(); } } 

In practice, however, there is nothing else between this and:

 if ( something ) { doA(); } else if ( something2 ) { doC(); } else { doD(); } 

Additional braces enclose a single statement, and you do not really need braces when the if or else controls are one expression. (My first example puts each statement with the exception of including if in braces.)

Logically, programmers are prone to being second; languages ​​where some binding ( {} , BEGIN / END or indentation) is required to almost always add elsif or elif to allow this second form. C and C ++ (and Java, and C #, and ...) do not, because the second form works without an additional keyword.

In the end, you don't need extra indentation. (I have cases with fifteen or twenty consecutive else if . That would do some serious indentation.) On the other hand, you really want a controlled expression on a separate line. (Attachment is optional: if your encoding standard sets the bracket separately to line, it is also common to suppress it if it contains only one expression.)

+5
source share

All Articles