TopMyGrade

GCSE/Computer Science/AQA

CS2.2Programming concepts: variables, constants, assignment, sequence, selection (IF), iteration (WHILE, FOR), input/output

Notes

Variables, sequence, selection and iteration

These four ideas are the heart of programming. Every algorithm — no matter how complex — is built from a combination of variables, sequences, selections (decisions) and iterations (loops).

Variables and constants

A variable is a named storage location whose value can change as the program runs.

SET score TO 0
SET score TO score + 1

A constant is a name whose value is fixed throughout execution. Use constants for values that should never change (e.g. VAT rate, pi). They protect you against accidental edits and document intent.

CONSTANT VAT TO 0.20
SET price TO net * (1 + VAT)

In AQA pseudocode, the syntax may also appear as vatRate ← 0.20. The arrow and SET ... TO are interchangeable.

Naming conventions — use clear, lower-case-camelCase names: totalScore, isLoggedIn, numberOfAttempts. Avoid single letters except for loop counters i, j.

Sequence

Statements run in the order written, top to bottom. This is the simplest construct.

USERINPUT name
USERINPUT age
OUTPUT "Hello " + name
OUTPUT "You are " + age

If you swap the first two lines, the meaning changes — sequence matters.

Selection (IF / ELSE)

A decision picks between alternatives based on a condition.

USERINPUT score
IF score >= 70 THEN
  OUTPUT "Distinction"
ELSEIF score >= 50 THEN
  OUTPUT "Pass"
ELSE
  OUTPUT "Fail"
ENDIF

Boolean conditions use comparison operators (=, !=, <, <=, >, >=) and logical operators (AND, OR, NOT).

IF age >= 18 AND hasLicense = true THEN
  OUTPUT "Can drive"
ENDIF

Iteration — counted (FOR) and conditional (WHILE)

A counted loop (FOR) repeats a fixed number of times.

FOR i FROM 1 TO 10
  OUTPUT i * i
ENDFOR

This prints 1, 4, 9, …, 100. The loop counter i runs from 1 to 10 inclusive.

A conditional loop (WHILE) repeats as long as a condition is true. The condition is checked before each iteration, so the body might not run at all if the condition is false initially.

USERINPUT pin
WHILE pin != 1234
  OUTPUT "Wrong PIN. Try again."
  USERINPUT pin
ENDWHILE
OUTPUT "Access granted"

If the user enters the right PIN first time, the loop body never executes.

A REPEAT … UNTIL loop (post-test) checks the condition after the body — so the body always runs at least once. AQA pseudocode uses REPEAT … UNTIL condition.

Choosing FOR or WHILE

  • You know how many times in advance → FOR.
  • The number depends on input or a condition during execution → WHILE.

E.g. iterating through every element of a 10-item array → FOR. Reading lines until a special "end" marker → WHILE.

Combining the constructs

SET total TO 0
SET count TO 0
USERINPUT n
WHILE n != 0
  IF n > 0 THEN
    SET total TO total + n
    SET count TO count + 1
  ENDIF
  USERINPUT n
ENDWHILE
IF count > 0 THEN
  OUTPUT "Average: " + (total / count)
ELSE
  OUTPUT "No positive numbers entered"
ENDIF

This nests selection inside a loop, then uses another selection to handle the edge case of no input.

Common pitfalls

  1. Off-by-one errors. FOR i FROM 1 TO 10 runs 10 times, not 9 or 11.
  2. Forgetting to update the loop variable in a WHILE — gives an infinite loop.
  3. Using = instead of == in some languages (Python: == for comparison; = for assignment). AQA pseudocode uses = for both, by context.
  4. Mixing AND/OR. age >= 18 AND age <= 65 is correct; age >= 18 OR age <= 65 is true for everyone.
  5. Reusing a variable name for two different purposes — confusing.

Try thisQuick check

Trace this program by hand for input 5:

USERINPUT n
SET total TO 0
FOR i FROM 1 TO n
  SET total TO total + i
ENDFOR
OUTPUT total

Output: 15 (1+2+3+4+5).

AI-generated · claude-opus-4-7 · v3-deep-computer-science

Practice questions

Try each before peeking at the worked solution.

  1. Question 14 marks

    Variable vs constant

    Define variable and constant with one example each.

    Ask AI about this

    AI-generated · claude-opus-4-7 · v3-deep-computer-science

  2. Question 24 marks

    Trace selection

    Trace the following for score = 65 and score = 35:

    IF score >= 70 THEN
      OUTPUT "Distinction"
    

    ELSEIF score >= 50 THEN

      OUTPUT "Pass"
    ELSE
      OUTPUT "Fail"
    ENDIF
    
    Ask AI about this

    AI-generated · claude-opus-4-7 · v3-deep-computer-science

  3. Question 34 marks

    FOR vs WHILE

    Explain when to use a FOR loop and when to use a WHILE loop, giving an example use case for each.
    
    Ask AI about this

    AI-generated · claude-opus-4-7 · v3-deep-computer-science

  4. Question 45 marks

    Sum of evens

    Write AQA pseudocode that outputs the sum of all even numbers from 2 to 20 inclusive.

    Ask AI about this

    AI-generated · claude-opus-4-7 · v3-deep-computer-science

  5. Question 53 marks

    Infinite loop bug

    A pupil writes:

    SET n TO 1
    WHILE n < 10
      OUTPUT n
    ENDWHILE
    

    Identify the bug and the fix.

    Ask AI about this

    AI-generated · claude-opus-4-7 · v3-deep-computer-science

  6. Question 65 marks

    Combine constructs

    Write pseudocode that asks the user to enter numbers one at a time. The program stops when the user enters 0, then outputs the average of the positive numbers entered.

    Ask AI about this

    AI-generated · claude-opus-4-7 · v3-deep-computer-science

  7. Question 73 marks

    Off-by-one

    What is wrong with this code, intended to print 1 to 10?

    FOR i FROM 0 TO 10
      OUTPUT i
    ENDFOR
    
    Ask AI about this

    AI-generated · claude-opus-4-7 · v3-deep-computer-science

Flashcards

CS2.2 — Programming concepts: variables, sequence, selection, iteration

12-card SR deck for AQA GCSE Computer Science topic CS2.2

12 cards · spaced repetition (SM-2)