Nested if Statements
引入本课内容
| English | 释义 | 拼音 |
|---|---|---|
| branch/bræntʃ/ | 分支 | fēn zhī |
| nested if/ˈnestɪd ɪf/ | 嵌套 if | qiàn tào if |
| condition evaluation | 条件求值 | tiáo jiàn qiú zhí |
| else-if ladder | else-if 阶梯结构 | else-if jiē tī jié gòu |
过宽的首个分支掩盖了最高等级
- 成绩分级结构先检查
s >= 60,再检查else if (s >= 90)。当 s=95 时,第一个条件为真,因此执行其分支,最高等级的分支根本不会被检查。 - 对于这些相互重叠的下限,应先检查较高的阈值。这是针对该分级结构的推理规则,并不是说所有可能的条件都必须按“最具体”到“最不具体”的顺序排列。

嵌套使决策依赖于其所在的路径
- 嵌套的 if 指在一个分支内放置另一个 if。如果内部语句位于外层条件为真的分支中,则只有外层条件为真时才会执行到它;如果 if 位于 else 中,则会在条件为假的路径上执行到它。
- 使用花括号明确这种结构。没有花括号时,else 会与最近的尚未匹配的 if 关联,这可能与缩进所暗示的结构不同。

阶梯结构选择第一个匹配的分支
if ... else if ... else阶梯结构从上到下检查条件,直到遇到第一个为真的条件。如果末尾有 else,条件求值完成后就会选中一个分支;如果没有 else,则可能没有分支被执行。- 下方方法接受普通整数成绩,并返回 A、B、C 或 F。对于 85,它先检查 90,再检查 80,返回 B,且不再对后续分支的条件求值。
public class GradeChoice {
public static String grade(int s) {
if (s >= 90) {
return "A";
} else if (s >= 80) {
return "B";
} else if (s >= 70) {
return "C";
} else {
return "F";
}
}
}
区分重叠与互斥的情况
- 当下限相互重叠时,
s >= 60包含所有满足s >= 90的成绩。将它放在首位会使第二个分支无法被选中,尽管 Java 可以编译该阶梯结构。 - 对于
x < 0、x == 0和x > 0这样的纯互斥条件,如果调整顺序时保持每个条件对应的结果不变,最终结果不一定会改变。而对于重叠的规则,可能会有意优先处理某种情况;应遵循题目给出的要求。
The first true branch wins
s = 85 takes the >= 80 branch (B); later branches are skipped.
In an else-if ladder, which condition's block runs?
难度 1/5 · 今日首次完成可获 100 金币及少量额外奖励
Java takes the first true condition top to bottom.
For if (s>=90) A; else if (s>=80) B; else C; with s = 85, the result is...
难度 1/5 · 今日首次完成可获 100 金币及少量额外奖励
85 fails >=90 but passes >=80 → B, then stop.
If a broad condition comes before a narrower one in a ladder, the narrower branch can become unreachable.
难度 1/5 · 今日首次完成可获 100 金币及少量额外奖励
The broad condition catches those cases first; put tightest first.
For an inner if placed inside the outer if's true branch, the inner condition is checked even when the outer condition is false.
难度 1/5 · 今日首次完成可获 100 金币及少量额外奖励
That inner statement is not reached on the false path. An if placed inside an else branch would have a different enclosing path.
For the shown grade ladder using s >= 90, s >= 80 and s >= 70, which ordering preserves the intended top grades?
难度 1/5 · 今日首次完成可获 100 金币及少量额外奖励
Higher lower bounds come first because these comparisons overlap. This is not a universal ordering rule for all Boolean conditions.
区分独立的 if 语句与阶梯结构
- 两条独立语句
if (s >= 60) ...; if (s >= 90) ...;在 s=95 时都会执行各自的主体代码。else-if 阶梯结构将这些备选分支连接起来,只选择第一个匹配的分支。 - 使用阈值处及刚好低于阈值的实际数值进行追踪:90 对应 A,89 对应 B,80 对应 B,79 对应 C,69 对应 F。只测试 85 会漏掉边界运算符的错误。
“第一个为真者胜出”描述的是相互连接的阶梯结构,而不是多个独立的 if 语句。如果前面的比较涵盖了某个分支的所有输入,该分支就可能无法被选中,但这不会导致编译时的不可达语句错误。

Two independent if statements test s >= 60 and s >= 90. For s=95, how many bodies run?
难度 2/5 · 今日首次完成可获 140 金币及少量额外奖励
Both conditions are true and the statements are independent, so both bodies run.
追踪外层 if 条件为假的分支
- 在
if (weekday) { ... } else { if (raining) { ... } }中,当 weekday 为 false 时,会执行到内部的 raining 检查。决定它何时运行的是它所在的分支,而不只是“嵌套”这个词。 - 预测执行路径前,先找出花括号、相互连接的备选分支、重叠条件和实际输入。保持代码采用的规则与边界测试一致;仅凭缩进不能确定 Java 如何将语句分组。
只有当控制流到达嵌套 if 所在的分支时,该 if 才会运行。相互连接的阶梯结构选择第一个为真的条件;独立的 if 语句可以执行多个主体代码。在这个特定的成绩分级结构中,较高的阈值排在前面,因为条件存在重叠。

With if (s >= 60) result = 1; else if (s >= 90) result = 2; else result = 3;, what is result for s = 95?
难度 2/5 · 今日首次完成可获 140 金币及少量额外奖励
95 satisfies the first comparison, so result becomes 1 and the else-if branch is skipped.
An if inside an outer else branch can be reached when the outer condition is false.
难度 1/5 · 今日首次完成可获 100 金币及少量额外奖励
Yes: the outer false path enters else, where the inner test may run.
出了什么问题?只有网站团队能看到这些信息。