有没有这样的情况:
if (condition) {
statement
} else {
if (condition2) {
statement
} else {
statement
}
}
不等同于:
if (condition) {
statement
} else if (condition2) {
statement
} else {
statement
}
如果有案例,我正在努力寻找案例。如果有人知道反例或者它们绝对等价,我们将不胜感激。
最佳答案
您问题中的两个陈述完全相同。也就是说,当您想在再次进入 if-else
block 之前执行一些常见步骤时,第一个语句更有用。例如:
if (condition) {
statement
} else {
perform some common steps.
if (condition2) {
statement
} else {
statement
}
}
关于java - Else If 逻辑等价,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41812640/