Search code examples
javaif-statementscopecurly-braces

Java: Is if , else if and else count as one statement?


Example:

if(Boolean){
    if(Boolean) something();
    else if(Boolean) something(); 
    else something();
           }

This is the same as

if(Boolean)
    if(Boolean) something();
    else if(Boolean) something(); 
    else something();

Is if, else if and else count as one statement?


Solution

  • An if/else pair is one statement. If you have if/else if/else, that's actually two if statements, with the second one being in the first one's else clause.

    if(Boolean)                                                 \
        something(); <- one statement                           |
    else                                                        | one
    {                                                           | if
        if(Boolean)                              \              | statement
            something(); <- one statement        | one if       |
        else                                     | statement    |
            something(); <- one statement        /              |
    }                                                           /