IF…THEN…ELSE…ENDIF (8.1b)

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

IF…THEN…ELSE…ENDIF (8.1b)

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 branch run?
A When the IF condition is FALSE
B When the IF condition is TRUE
2. Which line is written correctly in Cambridge pseudocode?
A ELSE Mark >= 50
B ELSE
3. Mark is 72. IF Mark >= 50 THEN OUTPUT "Pass" ELSE OUTPUT "Fail" ENDIF. What is output?
A Pass
B Pass and Fail
4. Mark is 38. IF Mark >= 50 THEN OUTPUT "Pass" ELSE OUTPUT "Fail" ENDIF. What is output?
A Fail
B Nothing is output
5. How many branches run each time an IF…ELSE…ENDIF is executed?
A Both, one after the other
B Exactly one

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 ATwo branches: Pass is output when Mark >= 50 is TRUE, otherwise Fail is output. Exactly one branch runs.
2ELSE OUTPUT "Fail" BENDIF is missing, so the IF...ELSE structure is never closed.
3ELSE lines up with its IF, and ENDIF closes the whole structure CThe ELSE branch, which runs only when the IF condition is FALSE and has no condition of its own.
4One result only: if the condition is FALSE, only the ELSE branch runs DELSE was given its own condition (Mark < 50 THEN). ELSE has no condition; it just runs when the IF condition is FALSE.
5IF Mark >= 50 THEN OUTPUT "Pass" ELSE Mark < 50 THEN OUTPUT "Fail" ENDIF EScore <- 30 IF Score >= 50 THEN OUTPUT "Pass" ELSE OUTPUT "Fail" ENDIF
6IF Mark >= 50 THEN OUTPUT "Pass" ELSE OUTPUT "Fail" FIF Age >= 16 THEN OUTPUT "Allowed" ELSE OUTPUT "Too young" ENDIF

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 Mark >= 50 THEN OUTPUT "Pass" ELSE OUTPUT "Fail" _____
3.
IF Score >= 40 ____ OUTPUT "Pass" ELSE OUTPUT "Fail" ENDIF
4.
IF Mark < 50 THEN OUTPUT "Fail" ELSE ______ "Pass" ENDIF
5.
__ Age >= 18 THEN OUTPUT "Adult" ELSE OUTPUT "Minor" ENDIF

Spot the Error

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

Question 1
A INPUT mark
B IF mark >= 50
C OUTPUT "Pass"
D ELSE
E OUTPUT "Fail"
F ENDIF
Question 2
A IF score > 40 THEN
B OUTPUT "Fail"
C ELSE
D OUTPUT "Pass"
E ENDIF
Question 3
A IF mark >= 50 THEN
B OUTPUT "Pass"
C ELSE mark < 50
D OUTPUT "Fail"
E ENDIF
Question 4
A INPUT age
B IF age >= 16 THEN
C ELSE:
D OUTPUT "Too young"
E ENDIF
Question 5
A IF mark >= 50 THEN
B result <- "Pass"
C ELSE
D result = "Fail"
E ENDIF

Answer Key

IF…THEN…ELSE…ENDIF (8.1b)
Section A: Quick Questions
Q1
A — When the IF condition is FALSE
Q2
B — ELSE
Q3
A — Pass
Q4
A — Fail
Q5
B — Exactly one
Section B: Match the Pairs
Q1
IF Mark >= 50 THEN OUTPUT "Pass" ELSE OUTPUT "Fail" ENDIF → Two branches: Pass is output when Mark >= 50 is TRUE, otherwise Fail is output. Exactly one branch runs.
Q2
ELSE OUTPUT "Fail" → The ELSE branch, which runs only when the IF condition is FALSE and has no condition of its own.
Q3
ELSE lines up with its IF, and ENDIF closes the whole structure → IF Age >= 16 THEN OUTPUT "Allowed" ELSE OUTPUT "Too young" ENDIF
Q4
One result only: if the condition is FALSE, only the ELSE branch runs → Score <- 30 IF Score >= 50 THEN OUTPUT "Pass" ELSE OUTPUT "Fail" ENDIF
Q5
IF Mark >= 50 THEN OUTPUT "Pass" ELSE Mark < 50 THEN OUTPUT "Fail" ENDIF → ELSE was given its own condition (Mark < 50 THEN). ELSE has no condition; it just runs when the IF condition is FALSE.
Q6
IF Mark >= 50 THEN OUTPUT "Pass" ELSE OUTPUT "Fail" → ENDIF is missing, so the IF...ELSE structure is never closed.
Section C: Definitions
Q1
ELSE — Adds a second branch that runs when the IF condition is FALSE, so exactly one of the two branches runs. ELSE has no condition of its own and lines up with its IF.
Section D: Fill in the Blanks
Q1
IF Mark >= 50 THEN OUTPUT "Pass" ELSE OUTPUT "Fail" ENDIF
Q2
IF Mark >= 50 THEN OUTPUT "Pass" ELSE OUTPUT "Fail" ENDIF
Q3
IF Score >= 40 THEN OUTPUT "Pass" ELSE OUTPUT "Fail" ENDIF
Q4
IF Mark < 50 THEN OUTPUT "Fail" ELSE OUTPUT "Pass" ENDIF
Q5
IF Age >= 18 THEN OUTPUT "Adult" ELSE OUTPUT "Minor" ENDIF
Section E: Spot the Error
Q1
B — The IF line is missing THEN. It should be IF mark >= 50 THEN so the true branch can start.
Q2
A — The condition is the wrong way round. High scores would output Fail and low scores Pass. It should be IF score < 40 THEN.
Q3
C — ELSE has no condition of its own. It runs automatically when the IF condition is FALSE, so the line should be just ELSE.
Q4
C — Cambridge pseudocode does not use a colon after ELSE. The keyword stands alone on its own line, lined up with its IF, so it should be just ELSE.
Q5
D — Assignment in Cambridge pseudocode uses the <- arrow, not =. It should be result <- "Fail".