Programming basics: variables, if statements and loops
Understand variables, data types, selection and loops, with pseudocode examples you can trace by hand.
KS3GCSEMiddle SchoolHigh SchoolCurriculum 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?
- i = 1: total = 1. i = 2: total = 3.
- i = 3: total = 6. i = 4: total = 10.
- 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'.
- Check the condition: 16 >= 18 is false.
- 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?
- x becomes 2, 4, 8, 16, then 32.
- 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
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.
Related guides
Binary search and bubble sort: searching and sorting algorithms
Learn how linear search, binary search and bubble sort work, with step-by-step traces and when to use each.
GCSEA-LevelHigh SchoolBinary numbers: convert binary to decimal and back
Learn how computers store data as bits and bytes, convert between binary and decimal and add binary numbers, with worked examples.
KS3GCSEMiddle SchoolHigh SchoolHow the internet works: IP addresses, DNS and packets
Learn what happens when you open a website, from DNS lookups to packets and HTTPS, in plain English.
KS3GCSEMiddle SchoolHigh SchoolSources and review
Reviewed by StudyMorrow editorial review on 5 Nov 2026.