Conditionals · 条件语句
| English | 中文 | Pinyin · 拼音 |
|---|---|---|
| conditional/kənˈdɪʃənl/ | 条件语句 | tiáo jiàn yǔ jù |
| block/blɒk/ | 代码块 | dài mǎ kuài |
| if/ɪf/ | 如果 | rú guǒ |
| else/els/ | 否则 | fǒu zé |
| clause/klɔːz/ | 子句 | zǐ jù |
| selection/sɪˈlekʃn/ | 选择 | xuǎn zé |
| iteration/ˌɪtəˈreɪʃn/ | 迭代 | dié dài |
| branch/bræntʃ/ | 分支 | fēn zhī |
| Boolean condition/ˈbuːlɪən kənˈdɪʃn/ | 布尔条件 | bù ěr tiáo jiàn |
Choosing what to do
- A conditional 条件语句 runs a block 代码块 of code only when a condition is true.
- This lets a program choose what to do based on data.
- Different inputs can send the program down different paths.
- It is one of the three building blocks of every program.
选择做什么
- 一个条件语句(conditional)只在条件为真时运行一代码块(block)。
- 这让程序基于数据选择做什么。
- 不同的输入能让程序走不同的路径。
- 它是每个程序的三个基本构件之一。
A conditional statement runs a block of code: · 一个条件语句运行一块代码:
The condition decides whether the block runs. · 条件决定块是否运行。
In an IF/ELSE, how many branches run each time? · 在一个 IF/ELSE 中,每次运行多少个分支?
The condition chooses one branch; the other is skipped. · 条件选一个分支;另一个被跳过。
IF and ELSE
- The basic form is an if 如果 statement, often with an else 否则 clause 子句:
- IF the condition is true, run the first block;
- ELSE (the condition is false), run the second block.
- Exactly one block runs each time.
IF 与 ELSE
- 基本形式是一个 if(如果)语句,常带一个 else(否则)子句(clause):
- IF 条件为真,运行第一个块;
- ELSE(条件为假),运行第二个块。
- 每次恰好运行一个块。
Selection: follow the IF / ELSE branch · 选择:跟随 IF / ELSE 分支
Drag the score and watch which branch runs. An IF tests the condition and takes the first block if true, otherwise the ELSE block — only one runs. · 拖动分数,看哪个分支运行。IF 测试条件,为真取第一个块,否则取 ELSE 块——只有一个运行。
Choosing between paths based on a condition is called . · 基于条件在路径间选择叫。
Selection is one of the three building blocks, with sequencing and iteration. · 选择是三个构件之一,另两个是顺序和迭代。
Selection
- This ability to choose between paths is called selection 选择.
- Selection is one of three building blocks, with sequencing and iteration 迭代.
- To understand a conditional, trace which branch 分支 runs for a given input.
- Only one branch runs, chosen by the condition.
选择
- 这种在路径间选择的能力叫选择(selection)。
- 选择是三个构件之一,另两个是顺序和迭代(iteration)。
- 要理解一个条件语句,追踪对给定输入哪个分支(branch)运行。
- 只有一个分支运行,由条件选定。
IF isMember: price ← price × 0.9. With isMember true and price 100, price becomes: · IF isMember:price ← price × 0.9。isMember 为真、价格 100 时,价格变成:
The IF branch runs: 100 × 0.9 = 90. · IF 分支运行:100 × 0.9 = 90。
If isMember is false, the ELSE branch runs and price stays 100. · 如果 isMember 为假,ELSE 分支运行,价格保持 100。
A false condition runs the ELSE block. · 一个假的条件运行 ELSE 块。
Writing the wrong Boolean condition usually causes: · 写错布尔条件通常造成:
A wrong condition sends the program down the wrong branch. · 错误的条件让程序走错分支。
Get the condition right
- Make sure the Boolean condition 布尔条件 correctly captures the intended rule.
- A wrong condition is a common source of logic errors.
Member discount. IF isMember is true: price ← price × 0.9; ELSE: price stays the same. With isMember true and price 100, the IF branch runs → price becomes 90. If false, the ELSE branch runs → price stays 100.
把条件写对
- 确保布尔条件(Boolean condition)正确地捕捉预期的规则。
- 一个错误的条件是逻辑错误的常见来源。
会员折扣。 IF isMember 为真:price ← price × 0.9;ELSE:price 保持不变。isMember 为真、价格 100 时,IF 分支运行 → 价格变成 90。若为假,ELSE 分支运行 → 价格保持 100。
A conditional runs a block only when a condition is true. An IF with an ELSE picks exactly one of two paths — this is selection, one of the three building blocks. Trace which branch runs for each input, and make sure the Boolean condition matches the rule you intended.
一个条件语句只在条件为真时运行一代码块。一个 IF 带 ELSE 恰好选两条路径之一——这是选择,三个构件之一。追踪每个输入哪个分支运行,并确保布尔条件匹配你预期的规则。