Every if...else if
example I’ve seen includes a final else
clause:
if (condition1) {
doA();
} else if (condition2) {
doB();
} else if (condition3) {
doC();
} else {
noConditionsMet();
}
alwaysDoThis();
I understand that this is basically syntactic sugar for nested if...else
statements:
if (condition1) {
doA();
} else {
if (condition2) {
doB();
} else {
if (condition3) {
doC();
} else {
noConditionsMet();
}
}
}
alwaysDoThis();
I have never seen any examples of an if...else if
that omits the last else
clause. But seeing as plain if
statements (without else
clauses) are valid, and going by the equivalent “nested statements” above, my gut tells me that this is okay to do:
if (condition1) {
doA();
} else if (condition2) {
doB();
} else if (condition3) {
doC();
}
alwaysDoThis();
Can someone point me to a resource or example that explicitly says whether or not it’s valid?
And on another level, if it is valid, would it be recommended or is it considered “bad practice”?