Variables, constants and assignment
A variable is a named place in memory that holds a value. The value can change while the program runs. A constant also has a name and a value, but the value is fixed for the whole program, for example CONST VAT β 0.2.
Assignment means storing a value in a variable. In pseudocode we write score β 10 (in Python: score = 10). The right side is worked out first, then stored on the left, so score β score + 1 adds one.
Meaningful identifiers
An identifier is the name you give a variable, constant or subroutine. Good names say what the value is: totalPrice is better than tp or x. Clear names make code easier to read, fix and share. Names cannot have spaces and usually cannot start with a digit.
Why use constants? If the tax rate changes, you edit one line, and you cannot change it by mistake later in the program.
Sequence: one step after another
Sequence means instructions run in the order they are written, top to bottom. Changing the order can change the answer.
x β 4 y β x Γ 2 OUTPUT y β 8
If you swap the first two lines, y is worked out before x has a value, so the program fails or gives a wrong answer.
Selection: IF, ELSE and CASE
Selection lets a program choose. A condition is a question that is either True or False, such as mark β₯ 50.
IF mark β₯ 50 THEN OUTPUT 'Pass' ELSE OUTPUT 'Try again' ENDIF
Only one branch runs. For more than two choices use ELSE IF (Python elif), or a CASE/match statement that checks one value against a list. Conditions use comparison operators (=, β , <, >, β€, β₯) and can be joined with AND, OR, NOT.
Iteration: definite and indefinite loops
Iteration means repeating. It saves writing the same lines again and again.
Definite (count-controlled) loop
The number of repeats is known before the loop starts. Use FOR.
FOR i β 1 TO 5 OUTPUT i ENDFOR β 1 2 3 4 5
Indefinite (condition-controlled) loop
The loop repeats until a condition changes, so the number of turns is not known in advance.
WHILE β¦ ENDWHILEchecks the condition at the start. If it is False at the start, the body runs zero times.REPEAT β¦ UNTIL(do-while) checks at the end, so the body always runs at least once.
total β 0 WHILE total < 20 total β total + 6 ENDWHILE OUTPUT total β 24
Typical uses: asking for a password until it is right, reading input until the user types 'stop'. Danger: if the condition never becomes False you get an infinite loop.
Nesting
Nesting means putting one control structure inside another: an IF inside a loop, a loop inside a loop, or an IF inside an IF. Indentation shows what is inside what.
FOR i β 1 TO 4
IF i MOD 2 = 0 THEN
OUTPUT i, ' even'
ELSE
OUTPUT i, ' odd'
ENDIF
ENDFORA loop inside a loop multiplies the work: an outer loop of 3 and an inner loop of 4 run the inner body 3 Γ 4 = 12 times. This is how you print a times-table grid or walk through every cell of a 2D array.
Try it: trace table at home
Take a sheet of paper and draw columns for each variable. Run the WHILE example from this page line by line, writing each new value in a new row. Then check your last row with the 3D at step 5. Next, change the rule to total β total + 7: predict how many turns it takes, then test it in the 3D idea or in any free online Python editor.
Key formulas and definitions
- Assignment: name β value (Python: name = value)
- Selection: IF condition THEN β¦ ELSE β¦ ENDIF
- Definite loop: FOR i β 1 TO n β¦ ENDFOR (runs n times)
- Indefinite loop: WHILE condition β¦ ENDWHILE (0 or more times)
- REPEAT β¦ UNTIL condition (1 or more times)
- Nested loops: total runs = outer count Γ inner count
Worked examples
1. What does this output? a β 3; b β a + 4; a β b Γ 2; OUTPUT a, b
Line by line: a = 3. b = 3 + 4 = 7. a = 7 Γ 2 = 14. Output: 14 7.
2. Write pseudocode that outputs 'Adult' if age is 18 or more, otherwise 'Child'.
INPUT age; IF age β₯ 18 THEN OUTPUT 'Adult' ELSE OUTPUT 'Child' ENDIF. Only one message is shown, because selection takes one branch.
3. How many times does the body run? FOR k β 2 TO 10
Count from 2 to 10 inclusive: 10 β 2 + 1 = 9 times.
4. Trace: n β 1; WHILE n < 50: n β n Γ 3. What is n at the end and how many turns?
1 β 3 (turn 1) β 9 (turn 2) β 27 (turn 3) β 81 (turn 4). 81 < 50 is False, so it stops. n = 81 after 4 turns.
5. Nested: FOR r β 1 TO 3, inside FOR c β 1 TO 4, OUTPUT '*'. How many stars?
Inner loop runs 4 times for each of the 3 outer turns: 3 Γ 4 = 12 stars.
6. Write a loop that keeps asking for a PIN until it equals 4321. Which loop type and why?
REPEAT INPUT pin UNTIL pin = 4321. It is indefinite (we do not know how many tries) and the user must be asked at least once, so REPEATβ¦UNTIL fits best.
Common mistakes
- Using a FOR loop when the number of repeats is unknown (e.g. until the user types 'stop'). Use WHILE or REPEATβ¦UNTIL.
- Forgetting to change the loop variable inside a WHILE loop, which makes an infinite loop.
- Thinking both branches of IFβ¦ELSE run. Only one branch runs each time.
- Off-by-one counting: FOR i β 1 TO 10 runs 10 times, but FOR i β 0 TO 10 runs 11 times.