I believe all of you, when learning C, have learned this syntax:
if (condition 1) {
statements
} else if (condition 2) {
statements
} ...
else {
statements
}
But after reading N1570, ยง 6.8.4.1 The if
statement, I find absolutely nothing talking about the chained else if
statements, unlike other languages that provide keywords like ElseIf
or elif
for this purpose.
According to my understanding, the whole if(...){...}else{...}
is one single statement (the else
clause may not be existent, which is irrelevant). So when it comes to parsing, as shown in below codes,
if (condition) {}
else
one_statement;
if (condition) {}
else
if (something else) {} else {}
The indented if
statement in the second block is equivalent to the indented one_statement;
in the first block, in terms of "a syntactic 'statement' unit".
Then, as C allows flexible spacing, any combination of an aggregate positive number of spaces, tabs and newlines are equivalent. So after re-spacing the above code, it turns into
if (condition) {
} else if (something else) {
} else {
}
Is my understanding correct?