Selection and Repetition · 选择与重复
| English | 中文 | Pinyin · 拼音 |
|---|---|---|
| selection/sɪˈlekʃn/ | 选择 | xuǎn zé |
| iteration/ˌɪtəˈreɪʃn/ | 迭代 | dié dài |
Beyond straight-line code
- So far, code has run top to bottom, one statement after another.
- Real algorithms need to choose and to repeat — not just run in a straight line.
- Selection 选择: pick which code to run based on a condition (
if). - Repetition (iteration 迭代): run some code many times (
while,for).
超越直线式代码
- 到目前为止,代码都是从上到下、一条语句接一条地运行。
- 真实的算法需要选择和重复——而不只是直线运行。
- 选择:根据一个条件挑选运行哪段代码(
if)。 - 重复(迭代):把某段代码运行许多次(
while、for)。
Selection: making decisions
- Selection lets a program take different paths depending on the data.
- "If the score is above $90$, print an A; otherwise print a B."
- The program branches — only one path runs each time.
- Unit 2 builds this with Boolean conditions and
ifstatements.
选择:做决定
- 选择让程序根据数据走不同的路径。
- “如果分数高于 $90$,打印 A;否则打印 B。”
- 程序会分支——每次只有一条路径运行。
- 第 2 单元用布尔条件和
if语句来构建这个。
Repetition: doing it again
- Repetition runs a block of code over and over until a condition says stop.
- "Keep asking for a password until the user types the right one."
- A loop saves you from copying the same code many times.
- Loops are how programs process lists, totals, and searches.
重复:再做一次
- 重复把一段代码一遍又一遍地运行,直到某个条件说停。
- “一直要求输入密码,直到用户输对为止。”
- 循环让你免于把同样的代码复制许多遍。
- 循环是程序处理列表、求和与搜索的方式。
Combining the three
- The three building blocks of every algorithm: sequence, selection, repetition.
- Sequence = do steps in order; selection = choose; repetition = repeat.
- Almost every program is these three, nested and combined.
- Mastering them lets you express any computation.
组合这三者
- 每个算法的三个构建块:顺序、选择、重复。
- 顺序 = 按序执行步骤;选择 = 挑选;重复 = 重复。
- 几乎每个程序都是这三者的嵌套与组合。
- 掌握它们让你能表达任何计算。
Tracing a loop · 追踪循环
Step through a counting loop and watch the running total build up. · 逐步遍历计数循环,观察累加总和的建立过程。
Selection and repetition sound similar but do different jobs. Selection (if) chooses whether a block runs once based on a condition; repetition (a loop) runs a block many times while a condition holds. A program combines sequence + selection + repetition — these three building blocks express every algorithm.
选择和重复听起来相似,却做不同的事。**选择(if)根据一个条件挑选一个块是否运行一次**;重复(循环)在一个条件成立期间把一个块运行许多次。程序组合顺序 + 选择 + 重复——这三个构建块表达每一个算法。
An algorithm for grading:
- Sequence: read the score.
- Selection: if
score >= 90, it's an A; else a B. - Repetition: do this for every student in the list.
一个评分算法:
- **顺序:**读取分数。
- **选择:**如果
score >= 90,是 A;否则是 B。 - **重复:**对列表里的每个学生都这样做。
Algorithms need more than sequence — they need selection (choose a path with if, based on a Boolean condition) and repetition/iteration (repeat a block with a loop). These three building blocks — sequence, selection, repetition — combine and nest to express any algorithm.
算法不只需要顺序——还需要选择(用 if 根据布尔条件挑选路径)和重复/迭代(用循环重复一个块)。这三个构建块——顺序、选择、重复——组合并嵌套,以表达任何算法。
Choosing which code to run based on a condition is called... · 基于条件选择运行哪段代码称为...
Selection (if) picks a path; repetition (loops) repeats a block. · 选择(if)挑选路径;重复(循环)重复一个代码块。
Running a block of code many times is called... · 多次运行一个代码块称为...
Repetition/iteration is done with while and for loops. · 重复/迭代通过while和for循环完成。
Which are the three building blocks of algorithms? · 算法的三个构建块是什么?
Sequence, selection, and repetition express any algorithm. · 序列、选择和重复表达任何算法。
An if statement chooses whether a block runs, while a loop can run a block many times. · if语句选择代码块是否运行,而循环可以多次运行一个代码块。
Selection = choose once; repetition = repeat. · 选择 = 选一次;重复 = 重复。
Running code repeatedly with a loop is called ___ (one word, synonym of repetition). · 用循环反复运行代码称为___(一个词,重复的同义词)。
Iteration is repetition via loops. · 迭代是通过循环实现的重复。