Skip to content
MathAnvil
§ Coding

Algorithms & Variables

§ Coding

Algorithms & Variables

LK20.103 min read

An algorithm is a finite sequence of precise, unambiguous instructions designed to solve a specific problem or perform a particular task. Variables serve as named storage locations that hold values which can change during algorithm execution. Together, they form the foundation of computational thinking and programming logic.

§ 01

Why it matters

Algorithms govern countless daily activities, from GPS navigation systems calculating the fastest route among 50,000 possible paths to online shopping platforms sorting through millions of products in under 0.2 seconds. Banking systems process over 150 billion transactions annually using algorithmic verification, whilst social media feeds use algorithms to select content from thousands of posts. In education, algorithms appear in KS3 computing curriculum and GCSE Computer Science specifications, building logical reasoning skills essential for problem-solving across disciplines. Modern search engines execute over 8.5 billion queries daily through sophisticated ranking algorithms, demonstrating how algorithmic thinking scales from simple classroom exercises to global infrastructure.

§ 02

How to solve algorithms & variables

Algorithms

  • An algorithm is a step-by-step set of instructions to solve a problem.
  • Must be precise, unambiguous, and have a clear end.
  • Flowcharts use shapes: oval (start/end), rectangle (process), diamond (decision).
  • Trace through algorithms with sample inputs to check correctness.

Example: Find max of a, b: if a > b → max = a, else max = b.

§ 03

Worked examples

Beginner§ 01

x = 9; x = x + 10. What is x?

Answer: 19

  1. Set initial value x = 9 x starts at 9.
  2. Add 10 x = 9 + 10 = 19 x becomes 19.
Easy§ 02

score = 14; if score <= 14: score = score - 1. What is score?

Answer: 13

  1. Check the condition Is 14 <= 14? Yes 14 <= 14 is true.
  2. Execute if true x = 13 Subtract 1: 14 - 1 = 13
Medium§ 03

x = 1; repeat 6 times: x = x * 2. What is x?

Answer: 64

  1. Trace each iteration 1 -> 2 -> 4 -> 8 -> 16 -> 32 -> 64 Double x, 6 times.
  2. Or use shortcut 1 x 26 = 64 Doubling 6 times is the same as 2^6.
§ 04

Common mistakes

  • Confusing assignment with equality: writing x = x + 5 when x = 3 and expecting the answer to be 8 = 3 + 5 instead of recognising that x becomes 8
  • Misunderstanding loop iterations: executing x = x * 2 starting from x = 3 for 4 loops and calculating 3 * 2 * 4 = 24 instead of tracing 3 → 6 → 12 → 24 → 48
  • Incorrect condition evaluation: with score = 15 and condition 'if score >= 15', concluding the condition is false instead of recognising that 15 >= 15 is true
§ 05

Frequently asked questions

What is the difference between an algorithm and a program?
An algorithm is a logical sequence of steps written in plain language or pseudocode, whilst a program is the algorithm implemented in a specific programming language. For example, 'add 10 to x' is algorithmic instruction, but 'x = x + 10' is program code.
How do you trace through an algorithm step by step?
Create a table showing variable values after each step. Start with initial values, then execute each instruction sequentially, updating the table. For x = 5; x = x * 3; x = x - 7, trace: x starts at 5, becomes 15, then becomes 8.
What does x = x + 1 actually mean in algorithms?
This instruction takes the current value of x, adds 1 to it, then stores the result back into x. If x was 12, it becomes 13. The equals sign means 'assign to' rather than mathematical equality.
Why do while-loops sometimes never stop?
A while-loop continues until its condition becomes false. If the condition never changes or changes incorrectly, the loop runs indefinitely. For example, 'while x > 0: x = x + 1' with x = 5 creates an infinite loop.
How do flowcharts represent different algorithm components?
Flowcharts use specific shapes: ovals for start/end points, rectangles for processes like 'x = x + 5', diamonds for decisions like 'is x > 10?', and parallelograms for input/output operations, connected by arrows showing flow direction.
§ 06

See also

§ 06

Related topics

Share this article