Conditionals
Introduced
9:10
Algorithms and Programming
Imagine a phone book with a million names, and you must find one. Check them one at a time, and you could be there all day. There is a way to find it in about…
English narration · English and 中文 subtitles you can switch
In this lesson
| English | Meaning |
|---|---|
| conditional/kənˈdɪʃənl/ | |
| block/blɒk/ | |
| if/ɪf/ | |
| else/els/ | |
| clause/klɔːz/ | |
| selection/sɪˈlekʃn/ | Choosing which branch of instructions to execute according to a condition. |
| iteration/ˌɪtəˈreɪʃn/ | Repeating a sequence of instructions or calculations. |
| branch/bræntʃ/ | |
| Boolean condition/ˈbuːlɪən kənˈdɪʃ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.
A conditional statement runs a block of code:
Difficulty 1/5 · 100 coins + a small bonus on first completion today
The condition decides whether the block runs.
In an IF/ELSE, how many branches run each time?
Difficulty 1/5 · 100 coins + a small bonus on first completion today
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.

Selection 选择 chooses between paths based on a condition
Selection: follow the IF / ELSE branch
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.
Choosing between paths based on a condition is called ______.
Difficulty 1/5 · 100 coins + a small bonus on first completion today
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.
IF isMember: price ← price × 0.9. With isMember true and price 100, price becomes:
Difficulty 1/5 · 100 coins + a small bonus on first completion today
The IF branch runs: 100 × 0.9 = 90.
If isMember is false, the IF branch runs and the member discount is applied.
Difficulty 1/5 · 100 coins + a small bonus on first completion today
False. A false condition skips the IF block and runs the ELSE branch, so the price stays 100.
Writing the wrong Boolean condition usually causes:
Difficulty 1/5 · 100 coins + a small bonus on first completion today
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.
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.
What went wrong? Only the site team sees this.