Programs make decisions: pass or fail, valid or invalid, which menu option was chosen. Selection lets a program test a condition and run different statements depending on the result, using IF statements, nested IFs and CASE statements.
Sequence means statements run in order, one after another. Selection means the program chooses which statements to run by testing a condition that is either TRUE or FALSE. An IF statement runs the THEN branch when the condition is TRUE and the optional ELSE branch when it is FALSE. A nested IF is an IF inside another IF. A CASE statement compares one variable with several single values and runs only the first match; OTHERWISE catches every other value.
In the lab, choose the Nested IF program and step through it. Change the input mark and watch which branch the program follows. THEN and ELSE are indented two spaces; the statements inside them are indented further.
DECLARE Age : INTEGER
INPUT Age
IF Age >= 18
THEN
OUTPUT "You can vote"
ELSE
OUTPUT "Too young to vote"
ENDIF
Age >= 18 is a Boolean expression: it is either TRUE or FALSE.The same idea in Python (for comparison only: the exam uses pseudocode)
age = int(input())
if age >= 18:
print("You can vote")
else:
print("Too young to vote")
Check the boundary. With IF Age > 18 an 18-year-old would be told they are too young. Use >= when the boundary value itself should pass.
An IF statement can sit inside the THEN or ELSE branch of another IF. Each inner level is indented further. You will not be asked to write more than three levels of nesting.
IF Mark >= 70
THEN
OUTPUT "Distinction"
ELSE
IF Mark >= 40
THEN
OUTPUT "Pass"
ELSE
OUTPUT "Fail"
ENDIF
ENDIF
Test the highest band first. If the program tested Mark >= 40 first, a mark of 85 would output Pass and never reach Distinction.
Four quick checks. Each correct answer earns XP and records this skill on your profile.
In the lab's Nested IF program with the input 55, what is output?
When does the ELSE branch of an IF statement run?
In a CASE statement, when does the OTHERWISE clause run?
Which keyword closes an IF statement in 0478 pseudocode?
When one variable is compared with several single values, a CASE statement is shorter and easier to read than a chain of IFs. Each case is indented two spaces. The cases are tested in order and only the first match runs.
DECLARE Choice : INTEGER INPUT Choice CASE OF Choice 1 : OUTPUT "Start game" 2 : OUTPUT "Load game" 3 : OUTPUT "Quit" OTHERWISE OUTPUT "Invalid choice" ENDCASE
The same idea in Python (for comparison only: the exam uses pseudocode)
choice = int(input())
if choice == 1:
print("Start game")
elif choice == 2:
print("Load game")
elif choice == 3:
print("Quit")
else:
print("Invalid choice")
OTHERWISE must be the last case, and it runs only if none of the cases above it match. Use an IF, not CASE, when a branch depends on a range such as Mark >= 40.
Unlocks once the four checks above are done. Worth more XP, written to IGCSE Paper 1 standard.
IF Temp > 30 THEN output "Hot", ELSE (IF Temp > 15 THEN output "Warm" ELSE output "Cold"). What is output when Temp is 30?
Why would a programmer use a CASE statement instead of several nested IF statements?
For the menu program in this lesson, the user enters 4. What is output?
Which condition is TRUE only for a mark from 0 to 100 inclusive?
This skill is now recorded on your profile. Keep the chain going.