IF…THEN…ELSE…ENDIF (7d)

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

IF…THEN…ELSE…ENDIF (7d)

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. The IF condition is FALSE. Which lines run?
A The lines after ELSE
B The lines after THEN
2. In IF...THEN...ELSE...ENDIF, how many branches run each time?
A Exactly one: THEN or ELSE
B Both THEN and ELSE
3. Which is the correct way to write the ELSE line?
A ELSE score < 50
B ELSE
4. mark ← 40 IF mark >= 50 THEN OUTPUT "Pass" ELSE OUTPUT "Fail" ENDIF What is output?
A Pass
B Fail
5. age ← 18 IF age < 18 THEN OUTPUT "Child" ELSE OUTPUT "Adult" ENDIF What is output?
A Adult
B Child

Match the Pairs

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

Left columnRight column
1IF Mark >= 50 THEN OUTPUT "Pass" ELSE OUTPUT "Fail" ENDIF AIF Number MOD 2 = 0 THEN OUTPUT "Even" ELSE OUTPUT "Odd" ENDIF
2ELSE IF Score >= 40 THEN BELSE was given its own condition and THEN. ELSE never has a condition. It should be written on its own line as just ELSE.
3ELSE has no condition of its own. It simply runs when the IF condition is FALSE. CIF Age >= 16 THEN OUTPUT "Allowed" ELSE OUTPUT "Not allowed" ENDIF
4Exactly one branch runs: THEN or ELSE, never both and never neither. DOutputs Pass if Mark is 50 or more, otherwise outputs Fail. Exactly one of the two lines runs.
5IF Mark >= 50 THEN OUTPUT "Pass" ELSE Mark < 50 THEN OUTPUT "Fail" ENDIF EThe ENDIF is missing. Every IF...THEN...ELSE structure must be closed with ENDIF.
6IF Mark >= 50 THEN OUTPUT "Pass" ELSE OUTPUT "Fail" FAdds a second condition that is only checked when the first IF condition was FALSE.

Definitions

ELSE

Fill in the Blanks

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

1.
IF Mark >= 50 THEN OUTPUT "Pass" ____ OUTPUT "Fail" ENDIF
2.
IF Temp > 30 ____ OUTPUT "Hot" ELSE OUTPUT "Not hot" ENDIF
3.
IF Score >= 70 THEN OUTPUT "A" ELSE __ Score >= 50 THEN OUTPUT "B" ELSE OUTPUT "C" ENDIF
4.
IF Stock _ 5 THEN OUTPUT "Reorder" ELSE OUTPUT "Stock OK" ENDIF
5.
IF Pin = Code THEN OUTPUT "Open" ELSE OUTPUT "Locked" _____

Spot the Error

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

Question 1
A IF Temp > 30 THEN
B OUTPUT "Hot"
C ELSE Temp <= 30
D OUTPUT "Cool"
E ENDIF
Question 2
A INPUT Mark
B IF Mark >= 50 THEN
C OUTPUT "Fail"
D ELSE
E OUTPUT "Fail"
F ENDIF
Question 3
A INPUT Num
B IF Num MOD 2 = 0 THEN
C OUTPUT "Even"
D ELSE THEN
E OUTPUT "Odd"
F ENDIF
Question 4
A INPUT Age
B IF Age >= 13
C OUTPUT "Allowed"
D ELSE
E OUTPUT "Too young"
F ENDIF
Question 5
A IF Mark >= 70 THEN
B OUTPUT "Merit"
C ELSE Mark >= 50 THEN
D OUTPUT "Pass"
E ENDIF

Answer Key

IF…THEN…ELSE…ENDIF (7d)
Section A: Quick Questions
Q1
A — The lines after ELSE
Q2
A — Exactly one: THEN or ELSE
Q3
B — ELSE
Q4
B — Fail
Q5
A — Adult
Section B: Match the Pairs
Q1
IF Mark >= 50 THEN OUTPUT "Pass" ELSE OUTPUT "Fail" ENDIF → Outputs Pass if Mark is 50 or more, otherwise outputs Fail. Exactly one of the two lines runs.
Q2
ELSE IF Score >= 40 THEN → Adds a second condition that is only checked when the first IF condition was FALSE.
Q3
ELSE has no condition of its own. It simply runs when the IF condition is FALSE. → IF Age >= 16 THEN OUTPUT "Allowed" ELSE OUTPUT "Not allowed" ENDIF
Q4
Exactly one branch runs: THEN or ELSE, never both and never neither. → IF Number MOD 2 = 0 THEN OUTPUT "Even" ELSE OUTPUT "Odd" ENDIF
Q5
IF Mark >= 50 THEN OUTPUT "Pass" ELSE Mark < 50 THEN OUTPUT "Fail" ENDIF → ELSE was given its own condition and THEN. ELSE never has a condition. It should be written on its own line as just ELSE.
Q6
IF Mark >= 50 THEN OUTPUT "Pass" ELSE OUTPUT "Fail" → The ENDIF is missing. Every IF...THEN...ELSE structure must be closed with ENDIF.
Section C: Definitions
Q1
ELSE — Starts the lines that run when the IF condition is FALSE. Exactly one branch runs: THEN or ELSE, never both. ELSE has no condition of its own.
Section D: Fill in the Blanks
Q1
IF Mark >= 50 THEN OUTPUT "Pass" ELSE OUTPUT "Fail" ENDIF
Q2
IF Temp > 30 THEN OUTPUT "Hot" ELSE OUTPUT "Not hot" ENDIF
Q3
IF Score >= 70 THEN OUTPUT "A" ELSE IF Score >= 50 THEN OUTPUT "B" ELSE OUTPUT "C" ENDIF
Q4
IF Stock < 5 THEN OUTPUT "Reorder" ELSE OUTPUT "Stock OK" ENDIF
Q5
IF Pin = Code THEN OUTPUT "Open" ELSE OUTPUT "Locked" ENDIF
Section E: Spot the Error
Q1
C — ELSE has no condition of its own. It runs automatically when the IF condition is FALSE. The line should be just ELSE.
Q2
C — The THEN branch runs when Mark >= 50 is TRUE, so it should output "Pass". Outputting "Fail" there gives the wrong message. The line should be OUTPUT "Pass".
Q3
D — THEN belongs only after the IF condition. ELSE has no condition and no THEN. The line should be just ELSE.
Q4
B — The IF line must end with THEN. It should be IF Age >= 13 THEN.
Q5
C — ELSE has no condition of its own. To test a second condition in a chain you need ELSE IF. The line should be ELSE IF Mark >= 50 THEN.