Is "else if" a single keyword?
C++SyntaxLanguage LawyerC++ Problem Overview
I am new to C++. I often see conditional statement like below:
if
statement_0;
else if
statement_1;
Question:
Syntactically, shall I treat else if
as a single keyword? Or is it actually an nested if
statement within the outer else
like below?
if
statement_0;
else
if
statement_1;
C++ Solutions
Solution 1 - C++
They are not a single keyword if we go to the draft C++ standard section 2.12
Keywords table 4
lists both if
and else
separately and there is no else if
keyword. We can find a more accessible list of C++ keywords by going to cppreferences section on keywords.
The grammar in section 6.4
also makes this clear:
selection-statement:
if ( condition ) statement
if ( condition ) statement else statement
The if
in else if
is a statement following the else
term. The section also says:
> [...]The substatement in a selection-statement (each substatement, in the > else form of the if statement) implicitly defines a block scope (3.3). > If the substatement in a selection-statement is a single statement and > not a compound-statement, it is as if it was rewritten to be a > compound-statement containing the original substatement.
and provides the following example:
if (x)
int i;
can be equivalently rewritten as
if (x) {
int i;
}
So how is your slightly extended example parsed?
if
statement_0;
else
if
statement_1;
else
if
statement_2 ;
will be parsed like this:
if
{
statement_0;
}
else
{
if
{
statement_1;
}
else
{
if
{
statement_2 ;
}
}
}
Note
We can also determine that else if
can not be one keyword by realizing that keywords are identifiers and we can see from the grammar for an identifier in my answer to Can you start a class name with a numeric digit? that spaces are not allowed in identifiers and so therefore else if
can not be a single keyword but must be two separate keywords.
Solution 2 - C++
Syntactically, it's not a single keyword; keywords cannot
contain white space. Logically, when writing lists of else if
, it's probably better if you see it as a single keyword,
and write:
if ( c1 ) {
// ...
} else if ( c2 ) {
// ...
} else if ( c3 ) {
// ...
} else if ( c4 ) {
// ...
} // ...
The compiler literally sees this as:
if ( c1 ) {
// ...
} else {
if ( c2 ) {
// ...
} else {
if ( c3 ) {
// ...
} else {
if ( c4 ) {
// ...
} // ...
}
}
}
but both forms come out to the same thing, and the first is far more readable.
Solution 3 - C++
No, it is not.
They are two keywords and, moreover, the second "if" is a substatement "inside" the scope determined by the first "else" statement.
Solution 4 - C++
You can see the scope by using curly braces:
if(X) {
statement_0;
}
else {
if(Y) {
statement_1;
}
}
And normally implemented with two distinct keywords, one is if and one is else.
Solution 5 - C++
As already answered, it isn't. They are two keywords. It's start of two statements one following each one other. To try make it a bit more clear, here's the BNF gramar which deal with if
and else
statements in C++ language.
statement:
labeled-statement
attribute-specifier-seqopt expression-statement
attribute-specifier-seqopt compound-statement
attribute-specifier-seqopt selection-statement
attribute-specifier-seqopt iteration-statement
attribute-specifier-seqopt jump-statement
declaration-statement
attribute-specifier-seqopt try-block
selection-statement:
if ( condition ) statement
if ( condition ) statement else statement
Note that statement
itself include selection-statement
. So, combinations like:
if (cond1)
stat
else if(cond2)
stat
else
stat
are possible and valid according to C++ standard/semantics.
Note: C++ grammar take from this page.
Solution 6 - C++
else and if are two different C++ keywords. An if statement can be followed by an optional else if...else statement. An if statement can have zero or more else if's and they must come before the else.
You can find syntax and example in this if...else statement tutorial
Solution 7 - C++
An if statement can be followed by an optional else if...else statement, which is very useful to test various conditions using single if...else if statement.
When using if , else if , else statements there are few points to keep in mind.
>An if can have zero or one else's and it must come after any else if's. > >An if can have zero to many else if's and they must come before the else. > >Once an else if succeeds, none of he remaining else if's or else's will be tested.
have a look if...else statement tutorial.
Solution 8 - C++
I would just like to add my point of view to all these explanations. As I see it, if you can use these keywords separately, they must be TWO keywords. Maybe you can have a look at c++ grammar, from this link in stackoverflow: https://stackoverflow.com/questions/2849795/is-there-a-standard-c-grammar
Regards