AeroLab Reading room

CS / Foundations

Algorithms, sequence and decisions

An algorithm is a finite, precise method for solving a defined problem. Sequence orders instructions, selection chooses branches and iteration repeats actions.

Learning objectives

The big idea

An algorithm is a finite, precise method for solving a defined problem. Sequence orders instructions, selection chooses branches and iteration repeats actions.

Trace an algorithm by recording variable values after each operation. Testing examples can reveal bugs, while a convincing correctness argument explains why all permitted inputs are handled.

Worked example

Start x=2, then set x←x+3, then x←2x. What is x?

Follow the solution

After addition x is 5; doubling gives 10.

Investigate and explain

Predict the worked result before calculating. Change one input, record the new result, and explain which assumptions still hold.

Use the prediction activity below; a local model appears only where it supports this topic.

Open the interactive investigation

Check your understanding

1. Which construct chooses between two actions?

  1. Selection
  2. Storage
  3. Translation
Check the answer and reasoning

Selection

An IF condition selects a branch based on a Boolean result.

2. Start x=2, then set x←x+3, then x←2x. What is x?

  1. 7
  2. 10
  3. 8
Check the answer and reasoning

10

After addition x is 5; doubling gives 10.

3. If a loop condition never becomes false, what may happen?

  1. Automatic correctness
  2. Guaranteed termination
  3. An infinite loop
Check the answer and reasoning

An infinite loop

A terminating loop needs progress toward a condition that ends repetition.