It is not only OK; I’d even recommend it, especially if conditionB
is relatively expensive to evaluate. For instance, here’s an alternative way:
if(conditionA) {
//do A stuff
} else if(conditionB && conditionB1) {
//do B1
} else if(conditionB && conditionB2) {
//do B2
} else if(conditionB && conditionB3) {
//do B3
}
Let’s say that conditionB
is evaluated by a method (like boolean getConditionB() {...}
) and getConditionB()
is expensive to execute. The JVM would have to execute your expensive method on every possibility, so your approach is better.
solved If statements inside else if [closed]