IF ... THEN ... ENDIF (7a)

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

IF ... THEN ... ENDIF (7a)

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. Which statement describes IF ... THEN ... ENDIF with no ELSE?
A The statements run only when the condition is TRUE
B The statements always run, whatever the condition is
2. Which is the correct way to close an IF block in Cambridge pseudocode?
A ENDIF
B END IF THEN
3. Which layout is correct? A: IF Score > 50 THEN OUTPUT "Pass" ENDIF B: IF Score > 50 THEN (newline) OUTPUT "Pass" (newline) ENDIF
A A
B B
4. Score is 40. What happens with this code? IF Score >= 50 THEN OUTPUT "Pass" ENDIF
A Nothing is output
B Fail is output
5. Age is 16. What happens with this code? IF Age >= 16 THEN OUTPUT "Can apply" ENDIF
A Nothing is output
B Can apply is output

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" ENDIF ACloses the IF block, marking where the statements that depend on the condition end.
2ENDIF BENDIF is missing, so the IF block is never closed. Add ENDIF on the last line.
3THEN goes on the same line as the condition and the statements to run go on the following indented lines. CIF Age >= 18 THEN OUTPUT "Adult" ENDIF
4An IF with no ELSE does nothing when the condition is FALSE and the program carries on after ENDIF. DOutputs Pass only when Mark is 50 or more; otherwise nothing is output.
5IF Temp > 30 OUTPUT "Hot" ENDIF ETHEN is missing after the condition. It should be IF Temp > 30 THEN.
6IF Lives = 0 THEN OUTPUT "Game over" FIF Score > 100 THEN Score <- 100 ENDIF

Definitions

IF condition THEN ... ENDIF

Fill in the Blanks

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

1.
__ Age >= 18 THEN OUTPUT "Adult" ENDIF
2.
IF Score > 50 ____ OUTPUT "Pass" ENDIF
3.
IF Temp < 10 THEN OUTPUT "Cold" _____
4.
IF Age __ 18 THEN OUTPUT "Can vote" ENDIF
5.
IF Lives = 0 THEN ______ "Game over" ENDIF

Spot the Error

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

Question 1
A INPUT Age
B IF Age >= 18 THEN
C PRINT "Adult"
D ENDIF
Question 2
A INPUT Score
B IF Score > 50 THEN
C OUTPUT "Pass"
D END IF
Question 3
A INPUT Mark
B Count ← 0
C IF Mark > 40
D Count ← Count + 1
E ENDIF
Question 4
A INPUT Temp
B Ice ← FALSE
C IF Temp ← 0 THEN
D Ice ← TRUE
E ENDIF
Question 5
A IF Lives == 0 THEN
B OUTPUT "Game over"
C ENDIF

Answer Key

IF ... THEN ... ENDIF (7a)
Section A: Quick Questions
Q1
A — The statements run only when the condition is TRUE
Q2
A — ENDIF
Q3
B — B
Q4
A — Nothing is output
Q5
B — Can apply is output
Section B: Match the Pairs
Q1
IF Mark >= 50 THEN OUTPUT "Pass" ENDIF → Outputs Pass only when Mark is 50 or more; otherwise nothing is output.
Q2
ENDIF → Closes the IF block, marking where the statements that depend on the condition end.
Q3
THEN goes on the same line as the condition and the statements to run go on the following indented lines. → IF Age >= 18 THEN OUTPUT "Adult" ENDIF
Q4
An IF with no ELSE does nothing when the condition is FALSE and the program carries on after ENDIF. → IF Score > 100 THEN Score <- 100 ENDIF
Q5
IF Temp > 30 OUTPUT "Hot" ENDIF → THEN is missing after the condition. It should be IF Temp > 30 THEN.
Q6
IF Lives = 0 THEN OUTPUT "Game over" → ENDIF is missing, so the IF block is never closed. Add ENDIF on the last line.
Section C: Definitions
Q1
IF condition THEN ... ENDIF — Runs the statements between THEN and ENDIF only when the condition is TRUE. If the condition is FALSE, they are skipped. This form has no ELSE part.
Section D: Fill in the Blanks
Q1
IF Age >= 18 THEN OUTPUT "Adult" ENDIF
Q2
IF Score > 50 THEN OUTPUT "Pass" ENDIF
Q3
IF Temp < 10 THEN OUTPUT "Cold" ENDIF
Q4
IF Age >= 18 THEN OUTPUT "Can vote" ENDIF
Q5
IF Lives = 0 THEN OUTPUT "Game over" ENDIF
Section E: Spot the Error
Q1
C — Cambridge pseudocode uses OUTPUT to display a message, not PRINT. The line should be OUTPUT "Adult".
Q2
D — The keyword that closes an IF block is the single word ENDIF, not END IF. The line should be ENDIF.
Q3
C — The IF line is missing THEN. The condition must be followed by THEN, so it should be IF Mark > 40 THEN.
Q4
C — A condition compares values, so it needs the relational operator =. The arrow ← is for assignment. The line should be IF Temp = 0 THEN.
Q5
A — Cambridge pseudocode tests equality with a single =, not ==. The line should be IF Lives = 0 THEN.