Tracing selection with a trace table

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

Tracing selection with a trace table

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. A trace table has x = 7. The algorithm says: if x > 10 then print "big". What do you record for the condition?
A TRUE, so print "big"
B FALSE, so skip the print line
2. Which step comes FIRST when you trace an if statement?
A Check the condition using the current values
B Run the lines inside the if block
3. score = 50. The code says: if score >= 50 then print "Pass". What goes in the condition column?
A FALSE
B TRUE
4. In an if-then-else, the condition is TRUE. What happens to the else branch?
A It is skipped
B It also runs after the then branch
5. age = 12. The code says: if age > 12 then print "Teen" else print "Kid". What is printed?
A Kid
B Teen

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 AThe condition was worked out wrongly. 3 > 5 is FALSE, so the then branch should be skipped.
2else B| age | age >= 13 | message |
3The trace table has a column to record the condition result for each check CBoth branches were followed. Only the branch that matches the condition result is followed.
4If the condition is FALSE, skip the then branch and do not change its variables in the table DThe branch that is skipped when the condition was TRUE, and followed only when it was FALSE
5x = 3, x > 5 is TRUE, so follow the then branch EWork out score >= 50 with the current value of score, then write TRUE or FALSE in the trace table
6temp = 30, temp > 25 is TRUE: do then AND else Fx <- 4 if x > 10 then x <- 0

Definitions

trace an if

Fill in the Blanks

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

1.
Trace row: score=45, score >= 50 is _____
2.
If the condition is _____ , skip the then branch
3.
x=4 | x > 10 | FALSE | then _______
4.
age=15 | age >= 18 | FALSE | ____ runs
5.
n <- 7 __ n > 5 then print "Big"

Spot the Error

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

Question 1
A score ← 45
B if score ← 50 then
C print "Pass"
D else
E print "Try again"
F end if
Question 2
A 31 ← temp
B if temp > 30 then
C print "Hot"
D end if
Question 3
A age ← 12
B if age < 13 then
C print Junior
D else
E print "Senior"
F end if
Question 4
A mark ← 72
B if mark ≥ 50 then
C print "Pass"
D end
E print "Done"
Question 5
A money ← 20
B if money ≥ 15 then
C money ← money - 15
D print "Bought
E end if

Answer Key

Tracing selection with a trace table
Section A: Quick Questions
Q1
B — FALSE, so skip the print line
Q2
A — Check the condition using the current values
Q3
B — TRUE
Q4
A — It is skipped
Q5
A — Kid
Section B: Match the Pairs
Q1
if score >= 50 then → Work out score >= 50 with the current value of score, then write TRUE or FALSE in the trace table
Q2
else → The branch that is skipped when the condition was TRUE, and followed only when it was FALSE
Q3
The trace table has a column to record the condition result for each check → | age | age >= 13 | message |
Q4
If the condition is FALSE, skip the then branch and do not change its variables in the table → x <- 4 if x > 10 then x <- 0
Q5
x = 3, x > 5 is TRUE, so follow the then branch → The condition was worked out wrongly. 3 > 5 is FALSE, so the then branch should be skipped.
Q6
temp = 30, temp > 25 is TRUE: do then AND else → Both branches were followed. Only the branch that matches the condition result is followed.
Section C: Definitions
Q1
trace an if — Check the condition using the current values, record TRUE or FALSE in the trace table, then follow only the branch that is true and skip the other branch.
Section D: Fill in the Blanks
Q1
Trace row: score=45, score >= 50 is FALSE
Q2
If the condition is FALSE , skip the then branch
Q3
x=4 | x > 10 | FALSE | then skipped
Q4
age=15 | age >= 18 | FALSE | else runs
Q5
n <- 7 if n > 5 then print "Big"
Section E: Spot the Error
Q1
B — The condition uses the assignment arrow ←, which stores a value. A condition has to compare, so it should be: if score ≥ 50 then. Without a comparison there is no TRUE or FALSE to record in the trace table.
Q2
A — The assignment is backwards. The variable name goes on the left of the arrow and the value on the right: temp ← 31. Without this, the trace table has no starting value for temp.
Q3
C — Text to be printed must be inside quotation marks. Without them, Junior looks like a variable name. It should be: print "Junior".
Q4
D — The selection block must be closed with end if. Without it, we can't tell where the true branch stops and where the code that always runs (print "Done") begins.
Q5
D — The text is missing its closing quotation mark. It should be: print "Bought".