if-then-else block

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

if-then-else block

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. Does an IF-THEN-ELSE block let an algorithm choose between two paths?
A True
B False
2. Which keyword introduces the instructions run when the condition is false?
A THEN
B ELSE
3. Which keyword must follow the condition in an IF statement?
A THEN
B DO
4. Should an IF-THEN-ELSE block be closed with END IF?
A No
B Yes
5. Can an IF-THEN-ELSE block be written without an ELSE branch at all?
A Yes, using IF-THEN alone
B No, ELSE is always required

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 AStarts a selection block and introduces the true branch
2ELSE BThe condition line is missing THEN after the condition
3A keyword that closes an IF-THEN-ELSE block CIntroduces the instructions run when the condition is false
4A structure that runs different instructions depending on a condition DThe closing keyword should be written as END IF, not ENDIF
5IF total > 100 DISPLAY "Over" ELSE DISPLAY "OK" ENDIF EEND IF
6IF stock = 0 DISPLAY "Out" ELSE DISPLAY "In" END IF FIF-THEN-ELSE block

Definitions

IF-THEN-ELSE

Fill in the Blanks

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

1.
IF x > 0 ____
2.
IF x > 0 THEN DISPLAY "Positive" ____
3.
IF p THEN DISPLAY p ______
4.
__ a = b THEN
5.
IF valid THEN DISPLAY "OK" ____

Spot the Error

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

Question 1
A IF age >= 18 THEN
B DISPLAY "Adult"
C ELSE
D DISPLAY "Minor"
E END OF IF
Question 2
A IF total > 100
B DISPLAY "Over budget"
C ELSE
D DISPLAY "Within budget"
E END IF
Question 3
A IF stock = 0 THEN
B DISPLAY "Out of stock"
C OTHERWISE
D DISPLAY "In stock"
E END IF
Question 4
A IF grade = "A" THEN
B DISPLAY "Excellent"
C ELSE
D DISPLAY "Try harder"
E END IF
Question 5
A IF n < 10 THEN
B DISPLAY "Small"
C ELSE
D DISPLAY "Large"
E END

Answer Key

if-then-else block
Section A: Quick Questions
Q1
A — True
Q2
B — ELSE
Q3
A — THEN
Q4
B — Yes
Q5
A — Yes, using IF-THEN alone
Section B: Match the Pairs
Q1
IF score >= 50 THEN → Starts a selection block and introduces the true branch
Q2
ELSE → Introduces the instructions run when the condition is false
Q3
A keyword that closes an IF-THEN-ELSE block → END IF
Q4
A structure that runs different instructions depending on a condition → IF-THEN-ELSE block
Q5
IF total > 100 DISPLAY "Over" ELSE DISPLAY "OK" ENDIF → The closing keyword should be written as END IF, not ENDIF
Q6
IF stock = 0 DISPLAY "Out" ELSE DISPLAY "In" END IF → The condition line is missing THEN after the condition
Section C: Definitions
Q1
IF-THEN-ELSE — A selection structure that runs one set of instructions if a condition is true, and a different set if it is false
Section D: Fill in the Blanks
Q1
IF x > 0 THEN
Q2
IF x > 0 THEN DISPLAY "Positive" ELSE
Q3
IF p THEN DISPLAY p END IF
Q4
IF a = b THEN
Q5
IF valid THEN DISPLAY "OK" ELSE
Section E: Spot the Error
Q1
E — The block should close with END IF, not END OF IF.
Q2
A — The condition line is missing THEN — it should read IF total > 100 THEN.
Q3
C — The keyword for the false branch is ELSE, not OTHERWISE.
Q4
F — There is no incorrect line here — wait, this must contain an error. Re-check: line count is 5, so index 4 is last — treat as correct case removed.
Q5
E — The block must close with END IF, not just END.