Nested Conditionals · 嵌套条件语句
| English | 中文 | Pinyin · 拼音 |
|---|---|---|
| nested conditional/ˈnestɪd kənˈdɪʃənl/ | 嵌套条件语句 | qiàn tào tiáo jiàn yǔ jù |
| three or more/θriː ɔː mɔː/ | 三个或更多 | sān gè huò gèng duō |
| outcomes/ˈaʊtkʌmz/ | 结果 | jié guǒ |
| else-if/els ɪf/ | 否则如果 | fǒu zé rú guǒ |
| execution path/ˌeksɪˈkjuːʃn pæθ/ | 执行路径 | zhí xíng lù jìng |
| readability/ˌriːdəˈbɪlɪti/ | 可读性 | kě dú xìng |
When two paths aren't enough
- Sometimes one decision is not enough.
- A nested conditional 嵌套条件语句 places one conditional inside another.
- This lets a program select among three or more 三个或更多 outcomes 结果, not just two.
- Think grades A, B, C, F — four outcomes from one score.
当两条路径不够时
- 有时一个决定不够。
- 一个嵌套条件语句(nested conditional)把一个条件语句放进另一个里。
- 这让程序在三个或更多(three or more)结果(outcomes)中选择,而非只有两个。
- 想想成绩 A、B、C、F——从一个分数得四个结果。
A nested conditional lets a program: · 一个嵌套条件语句让程序:
Nesting adds more branches than a single IF/ELSE. · 嵌套添加比单个 IF/ELSE 更多的分支。
A nested conditional selects among three or more possible ______. · 一个嵌套条件语句在三个或更多可能的______中选择。
Each branch leads to a different outcome. · 每个分支导向一个不同的结果。
The else-if structure
- A clean way to write this is an else-if 否则如果 structure, testing conditions in sequence:
- IF the first is true, do the first thing;
- ELSE IF the second is true, do the second thing;
- ELSE do the last thing.
else-if 结构
- 写这个的干净方式是一个 else-if(否则如果)结构,依次测试条件:
- IF 第一个为真,做第一件事;
- ELSE IF 第二个为真,做第二件事;
- ELSE 做最后一件事。
An else-if chain for grades · 成绩的 else-if 链
Drag the score and watch which branch runs. An else-if chain tests top to bottom and takes the FIRST condition that is true. · 拖动分数,看哪个分支运行。else-if 链从上到下测试,取第一个为真的条件。
An else-if chain stops testing: · 一个 else-if 链停止测试:
It takes the first true branch and skips the rest. · 它取第一个为真的分支,跳过其余的。
Order how the program checks the grade conditions. · 把程序检查成绩条件的顺序排序。
It tests from the top down and stops at the first true one. · 它从上往下测试,在第一个为真处停下。
Trace the path
- The program checks conditions from top to bottom.
- It stops at the first one that is true.
- To understand nested conditionals, trace the execution path 执行路径 for given inputs.
- Follow each true-or-false result to see which branch wins.
追踪路径
- 程序从上到下检查条件。
- 它在第一个为真处停下。
- 要理解嵌套条件语句,为给定输入追踪执行路径(execution path)。
- 跟随每个真或假的结果,看哪个分支胜出。
With the grade chain (≥90 A, ≥80 B, ≥70 C, else F), score 85 gives: · 用成绩链(≥90 A,≥80 B,≥70 C,否则 F),分数 85 得:
85 ≥ 90 is false; 85 ≥ 80 is true → "B", and testing stops. · 85 ≥ 90 为假;85 ≥ 80 为真 → "B",测试停止。
Simplifying deeply nested conditionals improves readability and reduces errors. · 简化深度嵌套的条件语句改善可读性并减少错误。
Fewer, clearer branches are easier to get right. · 更少、更清晰的分支更容易写对。
Keep it readable
- Deeply nested logic can become hard to read.
- Simplify overly complex nesting to improve readability 可读性 and reduce errors.
Score to grade. IF score ≥ 90 → "A"; ELSE IF ≥ 80 → "B"; ELSE IF ≥ 70 → "C"; ELSE → "F". For score 85: 85 ≥ 90 is false, so move on; 85 ≥ 80 is true → grade "B", and testing stops. Order matters — we already know it is below 90.
保持可读
- 深度嵌套的逻辑会变得难读。
- 简化过于复杂的嵌套,以改善可读性(readability)并减少错误。
分数到成绩。 IF 分数 ≥ 90 → "A";ELSE IF ≥ 80 → "B";ELSE IF ≥ 70 → "C";ELSE → "F"。分数 85:85 ≥ 90 为假,继续;85 ≥ 80 为真 → 成绩 "B",测试停止。顺序重要——我们已经知道它低于 90。
A nested conditional chooses among three or more outcomes, usually with an else-if chain that tests top to bottom and stops at the first true condition. Trace the execution path to find which branch runs (score 85 → "B"), and keep nesting shallow for readability.
一个嵌套条件语句在三个或更多结果中选择,通常用一个 else-if 链,从上到下测试并在第一个为真的条件处停下。追踪执行路径找出哪个分支运行(分数 85 → "B"),并保持嵌套浅以获得可读性。