Tracing an IF statement

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

Tracing an IF statement

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. Mark is 50. Tracing IF Mark >= 50 THEN, what is the condition?
A TRUE, because 50 is greater than or equal to 50
B FALSE, because 50 is not greater than 50
2. Mark is 50. Tracing IF Mark > 50 THEN, what is the condition?
A TRUE, because 50 is equal to 50
B FALSE, because 50 is not greater than 50
3. When tracing an IF with an ELSE, which statements do you follow?
A Only the THEN path or only the ELSE path, never both
B Both the THEN path and the ELSE path, one after the other
4. The condition is FALSE and the IF has no ELSE. What do you do in the trace table?
A Write a row for each statement inside the IF with blank values
B Skip the statements inside the IF and give them no row
5. Score is 40. IF Score >= 50 THEN OUTPUT "Pass" ELSE OUTPUT "Fail" ENDIF. What goes in the OUTPUT column?
A Fail
B Pass

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 AIF Age >= 13 THEN IF Age < 20 THEN
2ELSE OUTPUT "Fail" B50 > 50 is FALSE because > does not include the limit value, so the ELSE path is followed instead.
3Checking a limit: when Mark is 50, the condition is TRUE because >= includes the limit value CCheck the condition with the current value of Mark. If it is TRUE, follow the THEN path only.
4A nested IF is traced one condition at a time. The inner IF is only checked if the outer condition is TRUE DOnly one path is followed. When the condition is FALSE, only the ELSE statement is traced and the THEN statement gets no row.
5Mark = 50, IF Mark > 50 THEN: condition TRUE, so OUTPUT "Pass" EThis statement is skipped when the condition is TRUE, so it gets no row in the trace table.
6Mark = 40, IF Mark >= 50 THEN: FALSE, so trace both OUTPUT "Pass" and OUTPUT "Fail" FIF Mark >= 50 THEN

Definitions

IF Mark >= 50 THEN

Fill in the Blanks

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

1.
IF Mark __ 50 THEN OUTPUT "Pass" ENDIF
2.
IF Temp > 30 ____ OUTPUT "Hot" ENDIF
3.
IF Score < 40 THEN OUTPUT "Retry" _____
4.
Mark ← 50 IF Mark > 50 THEN // 50 > 50 is _____
5.
IF Mark >= 50 THEN __ Mark >= 80 THEN OUTPUT "Merit"

Spot the Error

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

Question 1
A Mark ← 50
B IF Mark >= 50 THEN
C OUTPUT Pass
D ENDIF
Question 2
A Score ← 72
B IF Score >= 50 THEN
C Grade ← "Pass"
D ELSE THEN
E Grade ← "Fail"
F ENDIF
Question 3
A Age ← 16
B IF Age > 17 THEN
C Adult = TRUE
D ELSE
E Adult ← FALSE
F ENDIF
Question 4
A IF Mark => 50 THEN
B Result ← "Pass"
C ELSE
D Result ← "Fail"
E ENDIF
Question 5
A Mark ← 85
B IF Mark >= 50
C IF Mark >= 80 THEN
D OUTPUT "Merit"
E ENDIF
F ENDIF

Answer Key

Tracing an IF statement
Section A: Quick Questions
Q1
A — TRUE, because 50 is greater than or equal to 50
Q2
B — FALSE, because 50 is not greater than 50
Q3
A — Only the THEN path or only the ELSE path, never both
Q4
B — Skip the statements inside the IF and give them no row
Q5
A — Fail
Section B: Match the Pairs
Q1
IF Mark >= 50 THEN → Check the condition with the current value of Mark. If it is TRUE, follow the THEN path only.
Q2
ELSE OUTPUT "Fail" → This statement is skipped when the condition is TRUE, so it gets no row in the trace table.
Q3
Checking a limit: when Mark is 50, the condition is TRUE because >= includes the limit value → IF Mark >= 50 THEN
Q4
A nested IF is traced one condition at a time. The inner IF is only checked if the outer condition is TRUE → IF Age >= 13 THEN IF Age < 20 THEN
Q5
Mark = 50, IF Mark > 50 THEN: condition TRUE, so OUTPUT "Pass" → 50 > 50 is FALSE because > does not include the limit value, so the ELSE path is followed instead.
Q6
Mark = 40, IF Mark >= 50 THEN: FALSE, so trace both OUTPUT "Pass" and OUTPUT "Fail" → Only one path is followed. When the condition is FALSE, only the ELSE statement is traced and the THEN statement gets no row.
Section C: Definitions
Q1
IF Mark >= 50 THEN — To trace an IF, work out if the condition is TRUE or FALSE for the current values, then follow only the THEN or ELSE path. Skipped statements get no row in the trace table.
Section D: Fill in the Blanks
Q1
IF Mark >= 50 THEN OUTPUT "Pass" ENDIF
Q2
IF Temp > 30 THEN OUTPUT "Hot" ENDIF
Q3
IF Score < 40 THEN OUTPUT "Retry" ENDIF
Q4
Mark ← 50 IF Mark > 50 THEN // 50 > 50 is FALSE
Q5
IF Mark >= 50 THEN IF Mark >= 80 THEN OUTPUT "Merit"
Section E: Spot the Error
Q1
C — The text to display must be in double quotes. Without them, Pass is read as a variable name. It should be OUTPUT "Pass". Mark is 50, so the condition is TRUE and the THEN path runs.
Q2
D — ELSE never has THEN after it. Only the IF line uses THEN. The line should be just ELSE. In the trace, ELSE is followed only when the condition is FALSE.
Q3
C — Assignment uses the arrow ←, not =. In pseudocode, = is for comparing values in a condition. It should be Adult ← TRUE. Age 16 makes the condition FALSE, so the ELSE row is the one recorded.
Q4
A — The 'greater than or equal to' operator is written >=, not =>. It should be IF Mark >= 50 THEN. A mark of exactly 50 makes this condition TRUE.
Q5
B — The IF line is missing THEN. It should be IF Mark >= 50 THEN. The outer condition is checked first. Only if it is TRUE is the nested IF reached.