Pseudocode IF ... THEN ... ELSE ... ENDIF

Tip: Choose Save as PDF in the print dialog to download. Use A4 paper size.

Pseudocode IF ... THEN ... ELSE ... ENDIF

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. Which IF statement is written correctly in Cambridge pseudocode?
A IF score >= 50 THEN OUTPUT "Pass" ENDIF
B IF score >= 50 THEN OUTPUT "Pass" END
2. Where should ENDIF be placed?
A Indented in line with the statements after THEN
B At the same indent as the IF it closes
3. What happens when the condition in an IF ... THEN ... ELSE ... ENDIF is false?
A The indented lines after THEN are run
B The indented lines after ELSE are run
4. What happens when an IF with no ELSE has a false condition?
A The program skips to the line after ENDIF
B The program stops with an error
5. Which condition correctly checks that age is not equal to 18?
A IF age <> 18 THEN
B IF age =! 18 THEN

Match the Pairs

Draw a line connecting each item on the left to its match on the right.

Left columnRight column
1IF score >= 50 THEN AIF temp > 30 THEN OUTPUT "Hot" ELSE OUTPUT "Not hot" ENDIF
2ELSE BStarts a selection: the indented lines below run only when score is 50 or more
3A one-way IF with no ELSE: nothing happens if the condition is false, and ENDIF closes it at the same indent as IF CThe ENDIF is missing, so the IF construct is never closed. Add ENDIF at the same indent as IF after the ELSE lines.
4A two-way IF with ELSE: one set of lines runs if the condition is true, a different set runs if it is false DIF age < 18 THEN OUTPUT "Under 18" ENDIF
5IF score >= 50 THEN OUTPUT "Pass" ELSE OUTPUT "Fail" EENDIF is indented as if it were inside the IF. It must line up with IF at the same indent.
6IF mark = 100 THEN OUTPUT "Full marks" ENDIF FIntroduces the lines that run when the IF condition is false

Definitions

IF THEN ELSE ENDIF

Fill in the Blanks

Complete each line of code by filling in the missing parts.

1.
IF Age >= 18 ____ OUTPUT "Adult" ENDIF
2.
IF Score __ 50 THEN OUTPUT "Pass" ENDIF
3.
IF Temp < 10 THEN OUTPUT "Cold" _____
4.
____ OUTPUT "Fail" ENDIF
5.
IF Pin __ 1234 THEN OUTPUT "Wrong PIN" ENDIF

Spot the Error

Shade in the letter of the line that contains an error.

Question 1
A IF score >= 50
B OUTPUT "Pass"
C ELSE
D OUTPUT "Fail"
E ENDIF
Question 2
A INPUT age
B IF age > 18 THEN
C OUTPUT "Minor"
D ELSE
E OUTPUT "Adult"
F ENDIF
Question 3
A INPUT guess
B IF guess = 7 THEN
C OUTPUT Correct
D ELSE
E OUTPUT "Wrong"
F ENDIF
Question 4
A INPUT mark
B IF mark => 40 THEN
C OUTPUT "Pass"
D ENDIF
Question 5
A IF lives ← 0 THEN
B OUTPUT "Game over"
C ENDIF

Answer Key

Pseudocode IF ... THEN ... ELSE ... ENDIF
Section A: Quick Questions
Q1
A — IF score >= 50 THEN OUTPUT "Pass" ENDIF
Q2
B — At the same indent as the IF it closes
Q3
B — The indented lines after ELSE are run
Q4
A — The program skips to the line after ENDIF
Q5
A — IF age <> 18 THEN
Section B: Match the Pairs
Q1
IF score >= 50 THEN → Starts a selection: the indented lines below run only when score is 50 or more
Q2
ELSE → Introduces the lines that run when the IF condition is false
Q3
A one-way IF with no ELSE: nothing happens if the condition is false, and ENDIF closes it at the same indent as IF → IF age < 18 THEN OUTPUT "Under 18" ENDIF
Q4
A two-way IF with ELSE: one set of lines runs if the condition is true, a different set runs if it is false → IF temp > 30 THEN OUTPUT "Hot" ELSE OUTPUT "Not hot" ENDIF
Q5
IF score >= 50 THEN OUTPUT "Pass" ELSE OUTPUT "Fail" → The ENDIF is missing, so the IF construct is never closed. Add ENDIF at the same indent as IF after the ELSE lines.
Q6
IF mark = 100 THEN OUTPUT "Full marks" ENDIF → ENDIF is indented as if it were inside the IF. It must line up with IF at the same indent.
Section C: Definitions
Q1
IF THEN ELSE ENDIF — Runs the indented lines after THEN if the condition is true, otherwise runs the ELSE lines. ENDIF closes the construct and sits at the same indent as IF. ELSE is optional.
Section D: Fill in the Blanks
Q1
IF Age >= 18 THEN OUTPUT "Adult" ENDIF
Q2
IF Score >= 50 THEN OUTPUT "Pass" ENDIF
Q3
IF Temp < 10 THEN OUTPUT "Cold" ENDIF
Q4
ELSE OUTPUT "Fail" ENDIF
Q5
IF Pin <> 1234 THEN OUTPUT "Wrong PIN" ENDIF
Section E: Spot the Error
Q1
A — The IF line is missing THEN. The condition must be followed by THEN, so it should be IF score >= 50 THEN.
Q2
B — The comparison operator is the wrong way round. Minor should be shown when age is less than 18, so the line should be IF age < 18 THEN.
Q3
C — The text to display is missing its quote marks. Without them Correct looks like a variable. It should be OUTPUT "Correct".
Q4
B — The comparison operator is written the wrong way round. 'Greater than or equal to' is >=, not =>.
Q5
A — ← is for assignment, not for comparing. A condition that tests equality uses =, so it should be IF lives = 0 THEN.