IF ... THEN ... ELSE ... ENDIF (7a)

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

IF ... THEN ... ELSE ... ENDIF (7a)

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. In IF ... THEN ... ELSE ... ENDIF, when does the ELSE part run?
A When the condition is FALSE
B When the condition is TRUE
2. How many paths run when an IF ... THEN ... ELSE ... ENDIF is executed once?
A Exactly one path
B Both paths, one after the other
3. Which is the correct way to lay out IF with ELSE in Cambridge pseudocode?
A IF Mark >= 50 THEN Result ← "Pass" ELSE Result ← "Fail" ENDIF
B IF Mark >= 50 THEN Result ← "Pass" ELSE Result ← "Fail" ENDIF
4. Which line must finish an IF ... THEN ... ELSE statement?
A ENDIF
B ENDELSE
5. Age is 12. What is the value of Correct after this code runs? IF Age >= 13 THEN Correct ← TRUE ELSE Correct ← FALSE ENDIF
A TRUE
B FALSE

Match the Pairs

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

Left columnRight column
1ELSE AGives an IF statement a second path that runs when the condition is FALSE
2IF Score >= 50 THEN Result ← "Pass" ELSE Result ← "Fail" ENDIF BIF Age >= 16 THEN OUTPUT "Adult" ELSE OUTPUT "Child" ENDIF
3When the condition is TRUE, the THEN statements run and the ELSE statements are skipped CExactly one path runs: Pass if Score is 50 or more, otherwise Fail
4In Cambridge pseudocode, THEN and ELSE statements are indented two spaces and ELSE lines up with IF DThe ENDIF is missing, so the IF ... THEN ... ELSE statement is never closed.
5IF Age >= 16 THEN OUTPUT "Adult" ELSE OUTPUT "Child" EIF Answer = 4 THEN Correct ← TRUE ELSE Correct ← FALSE ENDIF
6IF Answer = 4 THEN Correct ← TRUE ELSE Answer <> 4 THEN Correct ← FALSE ENDIF FELSE has a condition and a THEN added to it, but ELSE never takes a condition.

Definitions

ELSE

Fill in the Blanks

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

1.
____ Correct ← FALSE ENDIF
2.
ELSE Correct ← _____ ENDIF
3.
IF Guess = 7 ____ Correct ← TRUE ELSE
4.
IF Guess = 7 THEN Correct ← ____ ELSE
5.
ELSE Correct ← FALSE _____

Spot the Error

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

Question 1
A INPUT Age
B IF Age >= 18
C OUTPUT "Adult"
D ELSE
E OUTPUT "Child"
F ENDIF
Question 2
A INPUT Answer
B IF Answer = 12 THEN
C Correct = TRUE
D ELSE
E Correct ← FALSE
F ENDIF
Question 3
A INPUT Password
B IF Password == "cat" THEN
C OUTPUT "Welcome"
D ELSE
E OUTPUT "Denied"
F ENDIF
Question 4
A INPUT Temp
B IF Temp > 25 THEN
C OUTPUT "Hot"
D ELSE Temp <= 25
E OUTPUT "Cool"
F ENDIF
Question 5
A INPUT Number
B IF Number MOD 2 = 0 THEN
C OUTPUT "Odd"
D ELSE
E OUTPUT "Odd"
F ENDIF

Answer Key

IF ... THEN ... ELSE ... ENDIF (7a)
Section A: Quick Questions
Q1
A — When the condition is FALSE
Q2
A — Exactly one path
Q3
B — IF Mark >= 50 THEN Result ← "Pass" ELSE Result ← "Fail" ENDIF
Q4
A — ENDIF
Q5
B — FALSE
Section B: Match the Pairs
Q1
ELSE → Gives an IF statement a second path that runs when the condition is FALSE
Q2
IF Score >= 50 THEN Result ← "Pass" ELSE Result ← "Fail" ENDIF → Exactly one path runs: Pass if Score is 50 or more, otherwise Fail
Q3
When the condition is TRUE, the THEN statements run and the ELSE statements are skipped → IF Answer = 4 THEN Correct ← TRUE ELSE Correct ← FALSE ENDIF
Q4
In Cambridge pseudocode, THEN and ELSE statements are indented two spaces and ELSE lines up with IF → IF Age >= 16 THEN OUTPUT "Adult" ELSE OUTPUT "Child" ENDIF
Q5
IF Age >= 16 THEN OUTPUT "Adult" ELSE OUTPUT "Child" → The ENDIF is missing, so the IF ... THEN ... ELSE statement is never closed.
Q6
IF Answer = 4 THEN Correct ← TRUE ELSE Answer <> 4 THEN Correct ← FALSE ENDIF → ELSE has a condition and a THEN added to it, but ELSE never takes a condition.
Section C: Definitions
Q1
ELSE — Gives IF a second path. The THEN statements run when the condition is TRUE and the ELSE statements run when it is FALSE. Exactly one path runs, never both.
Section D: Fill in the Blanks
Q1
ELSE Correct ← FALSE ENDIF
Q2
ELSE Correct ← FALSE ENDIF
Q3
IF Guess = 7 THEN Correct ← TRUE ELSE
Q4
IF Guess = 7 THEN Correct ← TRUE ELSE
Q5
ELSE Correct ← FALSE ENDIF
Section E: Spot the Error
Q1
B — The IF line is missing THEN. It should be IF Age >= 18 THEN so the THEN statements can follow the condition.
Q2
C — Assignment uses the arrow ←, not =. The line should be Correct ← TRUE. In pseudocode = is used to compare values.
Q3
B — Cambridge pseudocode compares with a single =, not ==. The line should be IF Password = "cat" THEN.
Q4
D — ELSE has no condition. It simply runs when the IF condition is FALSE. The line should be just ELSE.
Q5
C — The THEN path runs when the condition is TRUE. When Number MOD 2 = 0 the number is even, so this line should be OUTPUT "Even".