Algorithms

Computer Science AS · Algorithm design and problem-solving. The idea in plain words, the rules to remember, the mistake to avoid and a worked example, written and checked by Mega Lecture.

Test this topic freeAll Computer Science AS topics

The idea

An algorithm is a solution to a problem written as a sequence of defined steps. It can be documented in structured English, as a flowchart or in pseudocode, and you must be able to convert between these forms.

Every algorithm is built from three constructs. Sequence is steps carried out one after another. Selection chooses a path with a condition (IF or CASE). Iteration repeats steps (FOR, WHILE or REPEAT).

Stepwise refinement starts with the main steps and breaks each one into smaller steps, again and again, until every step is detailed enough to be coded. Conditions are written as logic statements using AND, OR and NOT.

Rules to remember

Common mistake

In a range test students write OR in place of AND. Mark >= 40 OR Mark <= 59 is true for every mark. Test the condition with a value outside the range to check it.

Worked example

What is output by this algorithm?
Total ← 0
FOR Count ← 1 TO 4
   Total ← Total + Count * Count
NEXT Count
OUTPUT Total

  1. The loop runs four times, with Count = 1, 2, 3 and 4.
  2. Count * Count gives 1, 4, 9 and 16.
  3. Total after each pass: 0 + 1 = 1, 1 + 4 = 5, 5 + 9 = 14, 14 + 16 = 30.

Answer: 30

Ask about this

Type what you are stuck on. The helper finds the explanation, worked examples and the notes to read, free and instant.

Keep going

← Computational thinking skillsData types and records →Take a Computer Science AS test