3.3 Mathematical Expressions

10 阅读1分钟

1. Exam Points

  • Describe an algorithm using natural language.
  • Describe an algorithm using a flow chart.
  • Select the correct algorithm(s) based on given scenarios.
  • Algorithms to swap the values of two variables.(Use a third variable temp.)
  • Predict output or result based on a given algorithm.
  • Understand flow charts and figure out the algorithm represented by a flow chart.

2. Knowledge Points

(1) Algorithms (算法)

  • Programs incorporate iteration(迭代) and selection(选择) constructs(构造) to represent repetition and make decisions to handle varied input values.
  • An algorithm is a finite set of instructions that accomplish a specific task. To put it another way, an algorithm is a step-by-step process of solving a problem.
  • Algorithms can be represented using: Natural language, Diagrams, Pseudocode, Programming languages.
    • Natural language
      • Example:
        image.png
    • Diagrams (flowchart-流程图)
      • Example:
        image.png
    • Pseudocode
      • Example:
        image.png
    • Programming Languages
      • Example:
        image.png
  • Algorithms executed by programs are implemented using programming languages.
  • Every algorithm can be constructed using combinations of sequencing, selection, and iteration.
  • A code statement(语句) is a part of program code that expresses an action to be carried out.

(2) Flow Chart (流程图)

  • A flowchart is a diagram that shows the steps in a process.
  • Flowcharts are often used for visualizing the sequence of steps.
  • Symbols used in a flow chart:
    image.png
  • Example: find the largest among three numbers
    image.png

(3) Mathematical Expressions (数学表达式)

  • An expression is a combination of operators(运算符) and operands( 操作数).
  • Example:
    • 10 + 2 , 20 * 3.5, a + b / c, x
    • a > b, a < b, a>=b
    • a + findMax(b,c)
  • Operands(操作数) can be a value, a variable, a procedure call that returns a value.
  • An expression is evaluated to produce a single value.
    • a ← b is not an expression.
  • The evaluation of expressions follows a set order (precedence-优先级) of operations.
    • Ex. *,/ over +,-.
  • Arithmetic operators(算术运算符) include:
    • addition(+)
    • subtraction(-)
    • multiplication(*)
    • division(/) : 17 / 5 evaluates to 3.4
    • modulus operators (MOD) (取余) : 17 MOD 5 evaluates to 2
  • The MOD operator has the same precedence as the * and / operators.
  • For operators of the same precedence, evaluate from left to right.
    • Example: a * b / c

3. Exercises