GCSE Revision Aid: This resource is designed to support your revision and may contain errors. If you find a discrepancy with your class teaching, your teacher is correct — please let us know at gcserevise@scott.scottrix.co.uk.

CS7: Programming Constructs

Foundation Higher AQAEdexcelOCREduqasCCEA

Understanding variables, constants, sequence, selection (IF/CASE), and iteration (FOR/WHILE/REPEAT) - the building blocks of all programs.

Fastmail

📦 Variables and Constants

Variable: A named storage location in memory whose value can change during program execution. Constant: A named value that cannot be changed once it has been set.
Feature Variable Constant
Value can change? Yes No
When to use Values that need updating (score, counter) Fixed values (PI, tax rate, max attempts)
Example score ← 0, then score ← score + 1 CONST PI ← 3.14159
Advantage Flexible - can be updated Prevents accidental changes, makes code readable
Variables vs Constants
CONST VAT_RATE ← 0.20        // Constant - won't change
CONST MAX_SCORE ← 100        // Constant - won't change
score ← 0                    // Variable - will change
name ← ""                    // Variable - will change

score ← score + 10          // OK - variable can change
// VAT_RATE ← 0.25          // ERROR - constant cannot change

📝 Assignment

Assignment means storing a value in a variable. The variable name goes on the left, the value or expression goes on the right. The previous value is overwritten.
Assignment Examples
age ← 16                    // Assign the value 16
name ← "Bob"               // Assign the string "Bob"
total ← price * quantity   // Assign the result of an expression
counter ← counter + 1     // Increment: add 1 to current value
Key Rule: Assignment is NOT the same as mathematical equality.
counter ← counter + 1 means "take the current value of counter, add 1, and store the result back in counter". In maths, x = x + 1 has no solution!

➡️ Sequence

Sequence means executing instructions one after another in order, from top to bottom. This is the simplest programming construct - every program uses sequence.
Sequence Example
INPUT "Enter length: ", length
INPUT "Enter width: ", width
area ← length * width
OUTPUT "Area = ", area

Each line executes in order: input, input, calculate, output. No decisions, no repetition.

🔀 Selection

Selection allows a program to choose between different paths based on a condition. The three forms are: IF...THEN, IF...THEN...ELSE, and CASE/SWITCH.

IF...THEN

Simple IF
IF temperature > 30 THEN
    OUTPUT "It is hot today"
ENDIF

The body only executes if the condition is TRUE.

IF...THEN...ELSE

IF with ELSE
IF age >= 17 THEN
    OUTPUT "You can learn to drive"
ELSE
    OUTPUT "You cannot learn to drive yet"
ENDIF

One path if TRUE, another path if FALSE.

ELSEIF for Multiple Conditions

ELSEIF Chain
IF grade >= 90 THEN
    OUTPUT "A*"
ELSEIF grade >= 80 THEN
    OUTPUT "A"
ELSEIF grade >= 70 THEN
    OUTPUT "B"
ELSEIF grade >= 60 THEN
    OUTPUT "C"
ELSE
    OUTPUT "Below C"
ENDIF

CASE / SWITCH

CASE Statement
CASE OF day
    1 : OUTPUT "Monday"
    2 : OUTPUT "Tuesday"
    3 : OUTPUT "Wednesday"
    4 : OUTPUT "Thursday"
    5 : OUTPUT "Friday"
    OTHERWISE : OUTPUT "Weekend"
ENDCASE

Use CASE when checking a single variable against several specific values.

Feature IF...ELSE CASE
Best for Range checks, complex conditions Checking specific values of one variable
Conditions Can combine multiple variables One variable only
Example IF age >= 18 AND hasID THEN CASE OF menuChoice

🔄 Iteration

Iteration means repeating a block of code. There are three types: count-controlled (FOR), condition-controlled (WHILE), and post-condition (REPEAT...UNTIL).

FOR Loop (Count-Controlled)

FOR Loop
FOR i ← 1 TO 10
    OUTPUT i
NEXT i
// Outputs: 1, 2, 3, 4, 5, 6, 7, 8, 9, 10

Use FOR when you know exactly how many times you need to repeat.

FOR Loop with STEP
FOR i ← 10 TO 1 STEP -1
    OUTPUT i
NEXT i
// Outputs: 10, 9, 8, 7, 6, 5, 4, 3, 2, 1

WHILE Loop (Pre-condition)

WHILE Loop
password ← ""
WHILE password <> "secret"
    INPUT "Enter password: ", password
ENDWHILE
OUTPUT "Access granted"

Use WHILE when you don't know how many times to repeat, and the loop may need to execute zero times.

REPEAT...UNTIL (Post-condition)

REPEAT UNTIL Loop
REPEAT
    INPUT "Enter a number 1-10: ", num
UNTIL num >= 1 AND num <= 10

Use REPEAT when the loop body must execute at least once.

Feature FOR WHILE REPEAT...UNTIL
When to use Known number of repetitions Unknown, may run 0 times Unknown, must run at least once
Condition checked Automatically (counter) Before each iteration After each iteration
Can run zero times? No (if range is valid) Yes No
Infinite loop risk Low (counter handles it) High (must update condition) High (must update condition)

🪆 Nesting Constructs

Nesting means placing one construct inside another. You can put IF statements inside loops, loops inside IF statements, or loops inside other loops. This allows complex logic.
IF inside a FOR loop
FOR i ← 1 TO 20
    IF i MOD 2 = 0 THEN
        OUTPUT i, " is even"
    ELSE
        OUTPUT i, " is odd"
    ENDIF
NEXT i
WHILE loop inside an IF statement
INPUT difficulty
IF difficulty = "hard" THEN
    attempts ← 0
    WHILE attempts < 3
        INPUT "Guess the number: ", guess
        IF guess = secretNumber THEN
            OUTPUT "Correct!"
        ELSE
            OUTPUT "Wrong!"
        ENDIF
        attempts ← attempts + 1
    ENDWHILE
ENDIF
Exam Tip: When nesting, always match your opening and closing keywords. Every IF needs an ENDIF, every WHILE needs an ENDWHILE, and every FOR needs a NEXT. Indent your code to make the structure clear.

❓ Practice Questions

Q1: Explain the difference between a variable and a constant.

Q2: Write pseudo-code that asks for a number and outputs "Positive", "Negative", or "Zero" using selection.

Q3: When would you use a REPEAT...UNTIL loop instead of a WHILE loop?

Q4: Write pseudo-code using a FOR loop to output all multiples of 3 from 3 to 30.

Q5: Write pseudo-code that keeps asking for a password until the user enters "open123", then outputs "Welcome".

✅ Answers

  1. A variable is a named memory location whose value can change during execution. A constant is a named value that cannot be changed once set. Constants prevent accidental changes and make code more readable.
  2. INPUT number
    IF number > 0 THEN
        OUTPUT "Positive"
    ELSEIF number < 0 THEN
        OUTPUT "Negative"
    ELSE
        OUTPUT "Zero"
    ENDIF
  3. Use REPEAT...UNTIL when the loop body must execute at least once (e.g. input validation). Use WHILE when the loop may need to execute zero times (e.g. searching a list that might be empty).
  4. FOR i ← 3 TO 30 STEP 3
        OUTPUT i
    NEXT i
  5. password ← ""
    WHILE password <> "open123"
        INPUT "Enter password: ", password
    ENDWHILE
    OUTPUT "Welcome"

🎯 Exam Tips

⚠️ Common Errors

✗ Using IF instead of WHILE for repeated actions ✓ IF executes once based on a condition; WHILE repeats as long as the condition is true. Use IF for decisions, WHILE for repetition.

✗ Thinking FOR and WHILE loops are interchangeable in all situations ✓ FOR loops are for counted iteration (known number of repeats); WHILE loops are for conditional iteration (unknown number of repeats until a condition is met).

✗ Forgetting that WHILE checks the condition BEFORE the loop body, so it may never execute ✓ A WHILE loop is pre-tested: if the condition is FALSE initially, the loop body never executes. A REPEAT-UNTIL loop is post-tested and always executes at least once.

✗ Confusing ELSE and ELSEIF/ELIF ✓ ELSE is the default case when no IF or ELSEIF condition is true. ELSEIF/ELIF allows additional conditions to be checked in sequence after the initial IF.

✍️ Model Answer

Full-Mark Response

Write pseudo-code for a program that asks a user to enter a password. The password must be at least 8 characters long and contain at least one digit. The program should keep asking until a valid password is entered, then output 'Password accepted'. [5 marks]

valid ← FALSE WHILE valid = FALSE INPUT 'Enter password: ', password IF LENGTH(password) >= 8 THEN found_digit ← FALSE FOR i ← 0 TO LENGTH(password) - 1 IF password[i] >= '0' AND password[i] <= '9' THEN found_digit ← TRUE ENDIF NEXT i IF found_digit = TRUE THEN valid ← TRUE ELSE OUTPUT 'Password must contain at least one digit' ENDIF ELSE OUTPUT 'Password must be at least 8 characters' ENDIF ENDWHILE OUTPUT 'Password accepted'

📊 AO Deep Dive

Assessment Objective Analysis

AO1 (Computational Thinking — 40%): Demonstrate knowledge and understanding of the principles and concepts of computer science, including programming constructs: sequence, selection and iteration for AQA 8525, OCR J277 & Edexcel 1CP2.

AO2 (Application — 40%): Apply knowledge and understanding of computer science, including programming constructs: sequence, selection and iteration to analyse problems in computational terms and to design, write and evaluate solutions.

AO3 (Evaluation — 20%): Evaluate the effectiveness, correctness and efficiency of computational solutions, including programming constructs: sequence, selection and iteration, and make reasoned judgements about trade-offs.

📝 Exam Technique

GCSE Computer Science Exam Tips:
Choose the correct construct: IF for decisions, FOR for counted repetition (known number), WHILE for conditional repetition (unknown count). Always indent nested constructs clearly. For validation, use a WHILE loop with a Boolean flag. Show initialisation of variables BEFORE the loop. Remember: WHILE = pre-tested (may not execute), REPEAT-UNTIL = post-tested (always executes at least once). Use ENDIF, ENDWHILE to close each construct.

📝 Exam Questions by Topic

🎬 Video Resources

Share this page

Ready to ace your GCSE Computer Science exams?

Get the best revision books and guides to boost your grades.