Is “else if” a single keyword?

后端 未结 8 1984
忘了有多久
忘了有多久 2021-01-31 01:13

I am new to C++. I often see conditional statement like below:

if 
  statement_0;
else if
  statement_1;

Question:

Syntacticall

8条回答
  •  醉酒成梦
    2021-01-31 01:22

    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.

提交回复
热议问题