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

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

This IF ... THEN ... ELSE ... ENDIF (7c) worksheet is part of the IGCSE Computer Science 0478 — 7c Pseudocode and Flowcharts journey. Print it as a booklet?

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

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. Which line is correct pseudocode for the ELSE part of an IF statement?
A ELSE
B ELSE Answer >= 0 THEN
2. When the condition after IF is FALSE, what happens?
A The statements after THEN run
B The statements after ELSE run
3. In an IF ... THEN ... ELSE ... ENDIF statement, how many branches run each time?
A Both, one after the other
B Exactly one
4. Answer is 150. IF Answer < 0 OR Answer > 100 THEN Correct ← FALSE ELSE Correct ← TRUE ENDIF. What is Correct?
A FALSE
B TRUE
5. Answer is 50. IF Answer < 0 OR Answer > 100 THEN Correct ← FALSE ELSE Correct ← TRUE ENDIF. What is Correct?
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
1IF Mark >= 50 THEN OUTPUT "Pass" ELSE OUTPUT "Fail" ENDIF AIF Answer < 0 OR Answer > 100 THEN Correct ← FALSE ELSE Correct ← TRUE ENDIF
2ELSE BIF Age >= 18 THEN OUTPUT "Adult" ELSE OUTPUT "Child" ENDIF
3Check a range: set Correct to FALSE if Answer is outside 0 to 100, otherwise set it to TRUE CStarts the statements that run when the IF condition is FALSE; it has no condition of its own
4ELSE has no condition, so it is written on its own line with nothing after it DExactly one message is output: Pass when Mark is 50 or more, otherwise Fail
5IF Score > 10 THEN OUTPUT "High" ELSE Score <= 10 THEN OUTPUT "Low" ENDIF EThe ENDIF is missing, so the IF ... ELSE structure is never closed. ENDIF must be added after the ELSE branch.
6IF Answer < 0 OR Answer > 100 THEN Correct ← FALSE ELSE Correct ← TRUE FELSE has been given a condition and THEN. ELSE never has a condition, so it should just be ELSE on its own line.

Definitions

ELSE

Fill in the Blanks

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

1.
Correct ← FALSE ____ Correct ← TRUE
2.
IF Answer < 0 __ Answer > 100 THEN Correct ← FALSE
3.
ELSE Correct ← ____ ENDIF
4.
THEN Correct ← _____ ELSE
5.
ELSE OUTPUT "Well done" _____

Spot the Error

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

Question 1
A IF Answer < 0 OR Answer > 100
B THEN
C Correct = FALSE
D ELSE
E Correct ← TRUE
F ENDIF
Question 2
A INPUT Answer
B IF Answer < 0 AND Answer > 100
C THEN Correct ← FALSE
D ELSE Correct ← TRUE
E ENDIF
Question 3
A IF Mark >= 50
B THEN
C Result ← "Pass"
D ELSE Mark < 50
E Result ← "Fail"
F ENDIF
Question 4
A INPUT Number
B IF Number MOD 2 ← 0
C THEN OUTPUT "Even"
D ELSE OUTPUT "Odd"
E ENDIF
Question 5
A IF Answer < 0 OR > 100
B THEN
C Correct ← FALSE
D ELSE
E Correct ← TRUE
F ENDIF

Answer Key

IF ... THEN ... ELSE ... ENDIF (7c)
Section A: Quick Questions
Q1
A — ELSE
Q2
B — The statements after ELSE run
Q3
B — Exactly one
Q4
A — FALSE
Q5
A — TRUE
Section B: Match the Pairs
Q1
IF Mark >= 50 THEN OUTPUT "Pass" ELSE OUTPUT "Fail" ENDIF → Exactly one message is output: Pass when Mark is 50 or more, otherwise Fail
Q2
ELSE → Starts the statements that run when the IF condition is FALSE; it has no condition of its own
Q3
Check a range: set Correct to FALSE if Answer is outside 0 to 100, otherwise set it to TRUE → IF Answer < 0 OR Answer > 100 THEN Correct ← FALSE ELSE Correct ← TRUE ENDIF
Q4
ELSE has no condition, so it is written on its own line with nothing after it → IF Age >= 18 THEN OUTPUT "Adult" ELSE OUTPUT "Child" ENDIF
Q5
IF Score > 10 THEN OUTPUT "High" ELSE Score <= 10 THEN OUTPUT "Low" ENDIF → ELSE has been given a condition and THEN. ELSE never has a condition, so it should just be ELSE on its own line.
Q6
IF Answer < 0 OR Answer > 100 THEN Correct ← FALSE ELSE Correct ← TRUE → The ENDIF is missing, so the IF ... ELSE structure is never closed. ENDIF must be added after the ELSE branch.
Section C: Definitions
Q1
ELSE — Gives the statements that run when the IF condition is FALSE. ELSE has no condition of its own, so exactly one of the two branches (THEN or ELSE) runs.
Section D: Fill in the Blanks
Q1
Correct ← FALSE ELSE Correct ← TRUE
Q2
IF Answer < 0 OR Answer > 100 THEN Correct ← FALSE
Q3
ELSE Correct ← TRUE ENDIF
Q4
THEN Correct ← FALSE ELSE
Q5
ELSE OUTPUT "Well done" ENDIF
Section E: Spot the Error
Q1
C — Assignment in pseudocode uses the ← arrow. The = sign is used to compare values. It should be Correct ← FALSE.
Q2
B — No number is both below 0 and above 100, so AND is always FALSE and the ELSE branch would always run. The condition needs OR.
Q3
D — ELSE has no condition. It simply runs when the IF condition is FALSE. The line should be just ELSE.
Q4
B — The IF condition compares values, so it needs the comparison operator = and not the assignment arrow ←. It should be IF Number MOD 2 = 0.
Q5
A — Each side of OR must be a complete condition. Answer must be repeated: IF Answer < 0 OR Answer > 100.