Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is "else if" a single keyword?

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; 
like image 595
modeller Avatar asked Jun 23 '14 18:06

modeller


People also ask

Is else if one word?

The first elseif expression (if any) that evaluates to true would be executed. In PHP, you can also write 'else if' (in two words) and the behavior would be identical to the one of 'elseif' (in a single word).

Is else if a keyword in Java?

Yes, they are two separate keywords—the Java language specification does not specify an else if keyword. It is actually, as the other posters here have said, an if statement contained inside of an else statement.

Is else if the same as else if?

So as the default case handles all possibilities the idea behind else if is to split this whole rest into smaller pieces. The difference between else and else if is that else doesn't need a condition as it is the default for everything where as else if is still an if so it needs a condition.

What is else if?

Alternatively referred to as elsif, else if is a conditional statement performed after an if statement that, if true, performs a function.


2 Answers

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.

like image 186
Shafik Yaghmour Avatar answered Sep 20 '22 01:09

Shafik Yaghmour


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.

like image 27
James Kanze Avatar answered Sep 21 '22 01:09

James Kanze