Harout Tatarian Harout Tatarian - 3 months ago 7
Java Question

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?

Answer

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        /              |
}                                                           /
Comments