Skip to content

3.6 Conditionals 条件语句

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. Selection determines which parts of an algorithm are executed based on a condition being true or false.

    • 选择根据条件为真或假来确定算法的哪些部分被执行。
  2. Conditional statements, or "if-statements," affect the sequential flow of control by executing different statements based on the value of a Boolean expression.

    • 条件语句或"if语句"通过根据布尔表达式的值执行不同的语句来影响控制的顺序流。
  3. The exam reference sheet provides the following syntax for conditional statements:

    • 考试参考表提供以下条件语句语法:
    • Text:
      • 文本:
      IF (condition)
      {
        <block of statements>
      }
    • Block:
      • 块:
      IF (condition)
      [block of statements]
  4. The exam reference sheet provides the following syntax for IF-ELSE conditional statements:

    • 考试参考表提供以下IF-ELSE条件语句语法:
    • Text:
      • 文本:
      IF (condition)
      {
        <first block of statements>
      }
      ELSE
      {
        <second block of statements>
      }
    • Block: A visual representation showing the IF-ELSE structure
      • 块: 显示IF-ELSE结构的视觉表示
  5. In an IF statement, the code in block of statements is executed if the Boolean expression condition evaluates to true; no action is taken if condition evaluates to false.

    • 在IF语句中,如果布尔表达式条件计算为真,则执行语句块中的代码;如果条件计算为假,则不采取任何行动。
  6. In an IF-ELSE statement, the code in first block of statements is executed if the Boolean expression condition evaluates to true; otherwise, the code in second block of statements is executed.

    • 在IF-ELSE语句中,如果布尔表达式条件计算为真,则执行第一个语句块中的代码;否则,执行第二个语句块中的代码。

学生活动 Student Activities

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

    • 表达一个使用选择而不使用编程语言的算法。
  2. For selection:

    • 对于选择:
    • Write conditional statements.
      • 编写条件语句。
    • Determine the result of conditional statements.
      • 确定条件语句的结果。

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