Reputation: 135
First, please allow me to post a screenshot of the man page of GCC.
The sentence just above the second bundle of demo code confuses me, which is written as "so there is no way the 'else' could belong to the enclosing 'if'." As far as I try to get the idea of the author, the innermost if statement is if(b)
and foo();
. He intended to refer to if(b) when he said the "enclosing 'if'". But if my guess is right, the demo code should be like this:
{
if(a)
{
if(b)
foo();
}
else
bar();
}
Am I right? The question turns out to be what an "enclosing 'if' " is.
Upvotes: 1
Views: 482
Reputation: 11536
As far as I try to get the idea of the author, the innermost if statement is if(b) and foo();
Yes. And the else
is a clause of it, not a clause of the enclosing if.
He intended to refer to if(b) when he said the "enclosing 'if'".
No. S/he was discussing a means of making it clear that the else
belongs to the innermost if and not the enclosing one. Clear to the compiler, that is, so that it won't throw this warning.
Beyond that, the two examples are functionally identical. The only difference is that one will provoke a warning. Your version with braces is functionally different than those two -- it makes the else
a clause of the enclosing if(a)
.
Upvotes: 1
Reputation: 1469
The "enclosing if" is the if
in whose body another ("inner") if is "enclosed".
The sentence that confuses you is correct. The paragraph states, that you should put the parenthesis as shown in the second example to achieve the same result as in the first example (but without a warning from gcc
).
If you want to have code as it is suggested by the indentation of example 1, your example would be correct.
Now what the sentence means is: In example 1 the else
clause belongs to the inner if
, but this could only be by accident (when the behaviour in your example is desired).
When you write the code as it is in example 2, "there is no way" that you could have intended this.
Upvotes: 4