Skip to content

3.8 Iteration 迭代

The way statements are sequenced and combined in a program determines the computed result. Programs incorporate iteration and selection constructs to represent repetition and make decisions to handle varied input values.

  • 程序中语句的排序和组合方式决定了计算结果。程序结合迭代和选择结构来表示重复并做出决策来处理各种输入值。

核心要点 Core Points

  1. Iteration is a repeating portion of an algorithm. Iteration repeats a specified number of times or until a given condition is met.

    • 迭代是算法的重复部分。迭代重复指定的次数或直到满足给定条件。
  2. Iteration statements change the sequential flow of control by repeating a set of statements zero or more times, until a stopping condition is met.

    • 迭代语句通过重复一组语句零次或多次来改变控制的顺序流,直到满足停止条件。
  3. The exam reference sheet provides the following syntax for iteration:

    • 考试参考表提供以下迭代语法:
    • Text: REPEAT n TIMES { <block of statements> }
      • 文本: REPEAT n TIMES { <block of statements> }
    • Block: REPEAT n TIMES [block of statements]
      • 块: REPEAT n TIMES [block of statements]
    • In which the block of statements is executed n times.
      • 其中语句块被执行n次。
  4. The exam reference sheet provides the following syntax for REPEAT UNTIL iteration:

    • 考试参考表提供以下REPEAT UNTIL迭代语法:
    • Text:
      • 文本:
      REPEAT UNTIL (condition)
      {
        <block of statements>
      }
    • Block: A visual representation of the REPEAT UNTIL structure
      • 块: REPEAT UNTIL结构的视觉表示
    • In which the code in block of statements is repeated until the Boolean expression condition evaluates to true.
      • 其中语句块中的代码被重复,直到布尔表达式条件计算为真。
  5. In REPEAT UNTIL (condition) iteration, an infinite loop occurs when the ending condition will never evaluate to true.

    • 在REPEAT UNTIL (condition)迭代中,当结束条件永远不会计算为真时,会发生无限循环。
  6. In REPEAT UNTIL (condition) iteration, if the conditional evaluates to true initially, the loop body is not executed at all, due to the condition being checked before the loop.

    • 在REPEAT UNTIL (condition)迭代中,如果条件最初计算为真,循环体根本不会执行,因为条件在循环之前被检查。

学生活动 Student Activities

  1. Express an algorithm that uses iteration without using a programming language.

    • 表达一个使用迭代而不使用编程语言的算法。
  2. For iteration:

    • 对于迭代:
    • Write iteration statements.
      • 编写迭代语句。
    • Determine the result or side effect of iteration statements.
      • 确定迭代语句的结果或副作用。

基于 VitePress 构建的 AP CSP 学习平台