Else If Logical Equivalence

Is there ever a case when:

if (condition) {
    statement
} else {
    if (condition2) {
        statement
    } else {
        statement
    }
}

Not equivalent:

if (condition) {
    statement
} else if (condition2) {
    statement
} else {
    statement
}

If there is a case, I struggle to find a deal. If anyone knows an example counter, or if it is definitely equivalent, your input would be greatly appreciated.

+4
source share
5 answers

The two statements in your question are exactly the same. However, the first statement is more useful if you want to follow some general steps before entering the block again if-else. For instance:

if (condition) {
    statement
} else {
    perform some common steps.
    if (condition2) {
        statement
    } else {
        statement
    }
}
+2
source

Both conditional structures are equal.

, , - , , , .

if (condition1) {
    ...
} else {
    ...
    if (condition2) {
        ...
    } else {
        ...
    }
    ...
}

.

, .

+1

, .

, , , if; , - .. .

:

if (condition) {
    statement
} else {
    String foo = "bar";
    if (condition2) {
        statement
    } else {
        statement
    }
}

- if , .

, if, .

0

else , (, ), , java - , ,

0

, . : . , , . , , . , .

0

Source: https://habr.com/ru/post/1667570/


All Articles