AFAIK, if an "if" block is not provided the curly braces then only 1 statement is considered inside it. e.g.
if(..)
statement_1;
statement_2;
Irrespective of tabs, only statement_1
is considered inside the if
block.
Following code doesn't get along with that:
int main ()
{
if(false) // outer - if
if(false) // nested - if
cout << "false false
";
else if(true)
cout << "true
";
}
Above code doesn't print anything. It should have printed "true"
.
It appears as of the else if
is automatically nested inside the outer if
block. g++ -Wall
issues warning, but that is not the question here. Once you put the curly braces, everything goes fine as expected.
Why such different behavior ?
[GCC demo: without braces and with braces].
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…