Iteration · 迭代
| English | 中文 | Pinyin · 拼音 |
|---|---|---|
| loop/luːp/ | 循环 | xún huán |
| repeat/rɪˈpiːt/ | 重复 | chóng fù |
| Iteration/ˌɪtəˈreɪʃn/ | 迭代 | dié dài |
| until/ʌnˈtɪl/ | 直到 | zhí dào |
| infinite loop/ˈɪnfɪnət luːp/ | 无限循环 | wú xiàn xún huán |
| iterations/ˌɪtəˈreɪʃnz/ | 迭代过程 | dié dài guò chéng |
Loops repeat work
- Iteration 迭代 repeats a block of code using a loop 循环.
- Loops let a short program do a large amount of work.
- Instead of writing the same line 100 times, you loop it 100 times.
- It is the third building block, with sequencing and selection.
循环重复工作
- 迭代(iteration)用一个循环(loop)重复一块代码。
- 循环让短程序做大量的工作。
- 与其把同一行写 100 次,你把它循环 100 次。
- 它是第三个构件,另两个是顺序和选择。
Repeating a block of code with a loop is called . · 用一个循环重复一块代码叫。
Iteration lets a short program do a lot of work. · 迭代让短程序做大量工作。
A loop lets a short program do a large amount of repeated work. · 一个循环让短程序做大量重复的工作。
That is the whole point of iteration. · 这就是迭代的全部意义。
Two kinds of loop
- A repeat 重复 loop runs a block a fixed number of times — "REPEAT 10 TIMES" runs it ten times.
- A condition-controlled loop keeps running until 直到 a condition becomes true (or while one stays true).
- Its number of repetitions depends on the data, not a fixed count.
两种循环
- 一个重复(repeat)循环运行一个块固定次数——"REPEAT 10 TIMES" 运行它十次。
- 一个条件控制的循环持续运行,直到(until)一个条件变真(或当一个条件保持为真时)。
- 它的重复次数取决于数据,而非固定计数。
Match each loop to what controls its repetitions. · 把每种循环与控制它重复次数的东西配对。
REPEAT 10 TIMES is fixed; UNTIL depends on the data. · REPEAT 10 TIMES 是固定的;UNTIL 取决于数据。
An infinite loop happens when: · 无限循环发生在:
Something inside must move toward the stopping condition. · 内部必须有东西朝停止条件移动。
Beware the infinite loop
- A serious danger is an infinite loop 无限循环 — a loop that never meets its stopping condition.
- The program then runs forever.
- To avoid it, make sure something inside the loop moves toward the stopping condition.
- Usually that means a counter that changes each pass.
当心无限循环
- 一个严重的危险是无限循环(infinite loop)——一个永远不满足停止条件的循环。
- 程序于是永远运行。
- 要避免它,确保循环内部有东西朝着停止条件移动。
- 通常那意味着一个每遍改变的计数器。
Trace a loop, pass by pass · 一遍一遍地追踪一个循环
A trace table records each variable after every pass. Watch i climb while the running sum builds up, and see the loop stop once i passes 4. · 一个追踪表在每一遍后记录每个变量。看 i 爬升,同时运行总和累积,并看循环在 i 超过 4 后停止。
A loop adds 1, 2, 3, 4 to sum (starting at 0). What is the final sum? · 一个循环把 1、2、3、4 加到 sum(从 0 开始)。最终 sum 是多少?
1 + 2 + 3 + 4 = 10. · 1 + 2 + 3 + 4 = 10。
In the sum loop, which line keeps it from being an infinite loop? · 在求和循环中,哪一行让它不成为无限循环?
Without i = i + 1, i would never pass 4 and the loop would run forever. · 没有 i = i + 1,i 永远不会超过 4,循环会永远运行。
Trace the iterations
- To understand a loop, trace the variables across its iterations 迭代过程, one pass at a time.
Sum 1 to 4. Start sum = 0, i = 1. REPEAT UNTIL i > 4: add i to sum, then i = i + 1. Trace: sum 1 (i→2), 3 (i→3), 6 (i→4), 10 (i→5). Now 5 > 4, so it stops. Final sum = 10. The line i = i + 1 is what keeps it from being infinite.
追踪迭代过程
- 要理解一个循环,追踪变量在它的迭代过程(iterations)中,一遍一遍地。
求和 1 到 4。 开始 sum = 0,i = 1。REPEAT UNTIL i > 4:把 i 加到 sum,然后 i = i + 1。追踪:sum 1(i→2)、3(i→3)、6(i→4)、10(i→5)。现在 5 > 4,所以停止。最终 sum = 10。行 i = i + 1 是让它不变成无限循环的东西。
Iteration repeats a block with a loop, so a short program does a lot of work. A repeat loop runs a fixed count; a condition-controlled loop runs until a condition is met. Beware the infinite loop — always move toward the stop. Trace the variables across iterations (sum 1..4 = 10).
迭代用一个循环重复一块,所以短程序做大量工作。一个重复循环运行固定次数;一个条件控制的循环运行直到一个条件被满足。当心无限循环——总要朝停止移动。追踪变量在迭代过程中(求和 1..4 = 10)。