DRAFT
ElseAlwaysExecutes
Misconception:
if (c) A else B
is equivalent to if (c) A; B
.
Incorrect
The else branch of an if-else statement always executes
Correct
The else branch of an if-else statement only executes if the condition evaluates to false
CorrectionHere is what's right.
Here is what's right.
In an if-else-statement, only one of the two branches executes.
Simple Example
if (a) {
a();
} else {
b();
}
is different from
if (a) {
a();
}
b();
More Complex Example
if (a) {
a();
} else if (b) { // else means this condition only executes if `!a`
b();
} else {
c();
}
is different from
if (a) {
a();
} // no else, so next statement doesn't depend on `a`
if (b) {
b();
} else {
c();
}
Language
Java