💻 Computing

Programming basics: variables, if statements and loops

Understand variables, data types, selection and loops, with pseudocode examples you can trace by hand.

BeginnerUpdated 5 Nov 2026
KS3GCSEMiddle SchoolHigh School

Curriculum fit: KS3 and GCSE Computing / Computer Science (programming: variables, selection, iteration). US: no US standards source is linked here: check your school's computer science curriculum.

On this page

What is it?

Programs store data in variables, make decisions with selection (if statements) and repeat actions with iteration (loops). These three ideas are in almost every programming language.

Exam boards often use their own pseudocode, but the ideas are the same in languages such as Python.

Key formulas and ideas

  • Variable: a named place that stores a value that can change.
  • Data types: integer, float (decimal), string (text), boolean (true or false).
  • Selection: if condition then ... else ...
  • Iteration: a for loop repeats a set number of times; a while loop repeats while a condition is true.

Worked examples

Example 1: Trace a loop

Question: What is total after: total = 0; for i = 1 to 5: total = total + i?

  1. i = 1: total = 1. i = 2: total = 3.
  2. i = 3: total = 6. i = 4: total = 10.
  3. i = 5: total = 15.

Answer: 15

Example 2: Selection

Question: What does this print if age is 16? If age >= 18 then print 'adult' else print 'child'.

  1. Check the condition: 16 >= 18 is false.
  2. So the else branch runs.

Answer: child

Example 3: A while loop

Question: x = 1; while x < 20: x = x * 2. What is the final value of x?

  1. x becomes 2, 4, 8, 16, then 32.
  2. When x is 32 the condition x < 20 is false, so the loop stops.

Answer: 32

Common mistakes

  • Confusing = (assign) with == (compare) in many languages.
  • Creating an infinite loop by never changing the condition.
  • Off-by-one errors in loop ranges.
  • Mixing data types, such as adding text to a number.

Practice questions

1. total = 0; for i = 1 to 4: total = total + i. What is total?
2. A boolean can be…
3. Repeating instructions is called…
4. Which data type stores the text 'hello'?
5. A while loop stops when…

Frequently asked questions

Does the language matter?

The ideas are the same in Python, JavaScript and others. Only the syntax changes. Exam boards often use their own pseudocode style.

Does the language matter?

The ideas are the same in Python, JavaScript and others. Only the syntax changes.

Take this guide with you

Get a free printable PDF with the worked examples and quiz answers. Enter your email and the download starts straight away.

Sources and review

Reviewed by StudyMorrow editorial review on 5 Nov 2026.