CS7: Programming Constructs
Understanding variables, constants, sequence, selection (IF/CASE), and iteration (FOR/WHILE/REPEAT) - the building blocks of all programs.
Understanding variables, constants, sequence, selection (IF/CASE), and iteration (FOR/WHILE/REPEAT) - the building blocks of all programs.
| 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 |
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
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
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.
IF temperature > 30 THEN
OUTPUT "It is hot today"
ENDIF
The body only executes if the condition is TRUE.
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.
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 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 |
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 i ← 10 TO 1 STEP -1
OUTPUT i
NEXT i
// Outputs: 10, 9, 8, 7, 6, 5, 4, 3, 2, 1
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
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) |
FOR i ← 1 TO 20
IF i MOD 2 = 0 THEN
OUTPUT i, " is even"
ELSE
OUTPUT i, " is odd"
ENDIF
NEXT i
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
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".
INPUT number
IF number > 0 THEN
OUTPUT "Positive"
ELSEIF number < 0 THEN
OUTPUT "Negative"
ELSE
OUTPUT "Zero"
ENDIF
FOR i ← 3 TO 30 STEP 3
OUTPUT i
NEXT i
password ← ""
WHILE password <> "open123"
INPUT "Enter password: ", password
ENDWHILE
OUTPUT "Welcome"
✗ 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.
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'
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.
Get the best revision books and guides to boost your grades.