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.

In simpler words

An algorithm is a set of clear steps to solve a problem. You can write it in plain English, as a flowchart or in pseudocode. Every algorithm uses three things. Sequence means steps in order. Selection means choosing a path with IF. Iteration means repeating steps with a loop. Stepwise refinement breaks big steps into smaller ones until each step can be coded.

An everyday comparison

A recipe is an algorithm: steps in order (sequence), 'if the sauce is too thick, add water' (selection), 'stir until smooth' (iteration). A REPEAT loop is like tasting at least once before deciding if more salt is needed. The comparison stops working because a cook can judge vague words like 'smooth', while an algorithm needs exact conditions, such as Mark >= 40 AND Mark <= 59.

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.

In the exam

Start with an identifier table: name, data type, purpose. Write pseudocode with meaningful names and proper indentation. Choose the right loop and say why: FOR when the number of repeats is known, WHILE when the loop may run zero times, REPEAT when it must run at least once. Join range tests with AND and use brackets when mixing AND and OR. In flowcharts, use the correct symbols for input, process and decision.

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