Is “else if” a single keyword?

后端 未结 8 2057
忘了有多久
忘了有多久 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:30

    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.

提交回复
热议问题