IF…THEN…ENDIF (7d)

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

IF…THEN…ENDIF (7d)

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. Which line must close every IF statement in Cambridge pseudocode?
A ENDIF
B END
2. Score ← 40. IF Score > 50 THEN OUTPUT "Pass" ENDIF. What is output?
A Pass
B Nothing is output
3. Which IF statement is written correctly?
A IF Age >= 18 THEN OUTPUT "Adult"
B IF Age >= 18 THEN OUTPUT "Adult" ENDIF
4. Temp ← 35. IF Temp > 30 THEN OUTPUT "Hot" ENDIF. What is output?
A Hot
B Nothing is output
5. When does the code between THEN and ENDIF run?
A Only when the condition is TRUE
B Every time, whatever the condition is

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 OUTPUT "Pass" ENDIF AOutputs Pass only when Score is 50 or more; if Score is lower, the OUTPUT line is skipped
2ENDIF BThe IF has no ENDIF. Every IF must be closed with ENDIF after the lines that depend on the condition.
3The lines between THEN and ENDIF are indented so it is clear which lines depend on the condition CCloses the IF statement and marks where the conditional lines end
4When the condition is FALSE, the program skips the indented lines and carries on after ENDIF DCount ← 0 IF Count > 5 THEN OUTPUT "Big" ENDIF OUTPUT "Done"
5IF Temp > 30 THEN OUTPUT "Hot" EIF Age >= 18 THEN OUTPUT "Adult" ENDIF
6IF Score > 90 THEN OUTPUT "Top mark" ENDIF FThe line between THEN and ENDIF is not indented, so it is not clear that it depends on the condition.

Definitions

IF condition THEN ... ENDIF

Fill in the Blanks

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

1.
__ Age >= 16 THEN OUTPUT "Adult" ENDIF
2.
IF Score > 50 ____ OUTPUT "Pass" ENDIF
3.
IF Temp < 10 THEN OUTPUT "Cold" _____
4.
IF Total _ 100 THEN Discount ← 10 ENDIF
5.
IF Count > 0 THEN ______ Total / Count ENDIF

Spot the Error

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

Question 1
A IF score >= 50
B OUTPUT "Pass"
C ENDIF
Question 2
A IF temp ← 30 THEN
B OUTPUT "Exactly 30"
C ENDIF
Question 3
A total ← 0
B IF total == 0 THEN
C OUTPUT "Empty"
D ENDIF
Question 4
A INPUT age
B IF age >= 18 THEN
C OUTPUT "Adult"
D ENDWHILE
Question 5
A INPUT mark
B IF mark < 40 THEN
C PRINT "Retake"
D ENDIF

Answer Key

IF…THEN…ENDIF (7d)
Section A: Quick Questions
Q1
A — ENDIF
Q2
B — Nothing is output
Q3
B — IF Age >= 18 THEN OUTPUT "Adult" ENDIF
Q4
A — Hot
Q5
A — Only when the condition is TRUE
Section B: Match the Pairs
Q1
IF Score >= 50 THEN OUTPUT "Pass" ENDIF → Outputs Pass only when Score is 50 or more; if Score is lower, the OUTPUT line is skipped
Q2
ENDIF → Closes the IF statement and marks where the conditional lines end
Q3
The lines between THEN and ENDIF are indented so it is clear which lines depend on the condition → IF Age >= 18 THEN OUTPUT "Adult" ENDIF
Q4
When the condition is FALSE, the program skips the indented lines and carries on after ENDIF → Count ← 0 IF Count > 5 THEN OUTPUT "Big" ENDIF OUTPUT "Done"
Q5
IF Temp > 30 THEN OUTPUT "Hot" → The IF has no ENDIF. Every IF must be closed with ENDIF after the lines that depend on the condition.
Q6
IF Score > 90 THEN OUTPUT "Top mark" ENDIF → The line between THEN and ENDIF is not indented, so it is not clear that it depends on the condition.
Section C: Definitions
Q1
IF condition THEN ... ENDIF — Runs the indented lines between THEN and ENDIF only when the condition is TRUE. If it is FALSE, those lines are skipped. Every IF must be closed with its own ENDIF.
Section D: Fill in the Blanks
Q1
IF Age >= 16 THEN OUTPUT "Adult" ENDIF
Q2
IF Score > 50 THEN OUTPUT "Pass" ENDIF
Q3
IF Temp < 10 THEN OUTPUT "Cold" ENDIF
Q4
IF Total > 100 THEN Discount ← 10 ENDIF
Q5
IF Count > 0 THEN OUTPUT Total / Count ENDIF
Section E: Spot the Error
Q1
A — The IF line is missing the keyword THEN. The condition must be followed by THEN: IF score >= 50 THEN.
Q2
A — The ← symbol assigns a value. A condition needs a comparison operator, so it should be IF temp = 30 THEN.
Q3
B — Cambridge pseudocode compares with a single =, not ==. The line should be IF total = 0 THEN.
Q4
D — An IF must be closed with ENDIF. ENDWHILE closes a WHILE loop, so the last line should be ENDIF.
Q5
C — Cambridge pseudocode uses OUTPUT to display a value, not PRINT. The indented line inside the IF should be OUTPUT "Retake".