Nested if blocks in pseudocode

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

Nested if blocks in pseudocode

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. In a nested if, when is the inner condition checked?
A Only when the outer condition is true
B Every time, whether the outer condition is true or not
2. Which pseudocode shows the inner block indented correctly?
A if age >= 12 then if hasTicket = true then print "Enter" end if end if
B if age >= 12 then if hasTicket = true then print "Enter" end if end if
3. A nested if has 2 if blocks, one inside the other. How many end if lines does it need?
A 2, one for each if
B 1, because they are joined together
4. Which is the correct order for closing a nested if?
A Close the outer if first, then the inner if
B Close the inner if first, then the outer if
5. score = 40. Outer if: score >= 50. Inner if: score = 100. What happens to the inner if?
A It is skipped, because the outer condition is false
B It is checked, and found to be false

Match the Pairs

Draw a line connecting each item on the left to its match on the right.

Left columnRight column
1if age >= 12 then if hasTicket = true then ANo indenting, so it is hard to see that the second if is nested inside the first.
2 end if end if BEach if block gets its own end if. The inner one closes first, then the outer one
3The inner block is indented so you can see which if it belongs to Cif raining = true then if haveUmbrella = true then print "Take umbrella" end if end if
4A nested if can do the same job as AND, because both conditions must be true for the action to run DThe outer if has no end if. Every if, including the outer one, needs its own end if.
5if age >= 12 then if hasTicket = true then print "Enter" end if EA nested if: the inner if is written inside the outer if, so hasTicket is only checked when age >= 12 is true
6if temp > 30 then if humid = true then print "Stay inside" end if end if Fif score > 50 then if score > 90 then print "Top mark" end if end if

Definitions

nested if

Fill in the Blanks

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

1.
if age >= 12 then __ tall = true then print "Ride" end if end if
2.
if age >= 12 then if tall = true then print "Ride" end if ______
3.
if score __ 50 then if score >= 80 then print "Merit" end if end if
4.
if age >= 12 ___ tall = true then print "Ride" end if
5.
if raining = true then if windy = true ____ print "Coat" end if end if

Spot the Error

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

Question 1
A if age >= 12 then
B if has_ticket = true
C print "Enter"
D end if
E end if
Question 2
A if score >= 50 then
B if score >= 90 than
C print "Excellent"
D end if
E end if
Question 3
A if temp > 30 then
B print "Hot day"
C if humidity => 70 then
D print "Muggy"
E end if
F end if
Question 4
A if logged_in = true then
B if is_admin = true then
C say "Admin panel"
D end if
E end if
Question 5
A if temp > 25 then
B print "Warm"
C if temp > 35 then
D print "Very hot
E end if
F end if

Answer Key

Nested if blocks in pseudocode
Section A: Quick Questions
Q1
A — Only when the outer condition is true
Q2
B — if age >= 12 then if hasTicket = true then print "Enter" end if end if
Q3
A — 2, one for each if
Q4
B — Close the inner if first, then the outer if
Q5
A — It is skipped, because the outer condition is false
Section B: Match the Pairs
Q1
if age >= 12 then if hasTicket = true then → A nested if: the inner if is written inside the outer if, so hasTicket is only checked when age >= 12 is true
Q2
end if end if → Each if block gets its own end if. The inner one closes first, then the outer one
Q3
The inner block is indented so you can see which if it belongs to → if score > 50 then if score > 90 then print "Top mark" end if end if
Q4
A nested if can do the same job as AND, because both conditions must be true for the action to run → if raining = true then if haveUmbrella = true then print "Take umbrella" end if end if
Q5
if age >= 12 then if hasTicket = true then print "Enter" end if → The outer if has no end if. Every if, including the outer one, needs its own end if.
Q6
if temp > 30 then if humid = true then print "Stay inside" end if end if → No indenting, so it is hard to see that the second if is nested inside the first.
Section C: Definitions
Q1
nested if — An if block written inside another if block. The inner condition is only checked when the outer condition is true. Indent the inner block and give each if its own end if.
Section D: Fill in the Blanks
Q1
if age >= 12 then if tall = true then print "Ride" end if end if
Q2
if age >= 12 then if tall = true then print "Ride" end if end if
Q3
if score >= 50 then if score >= 80 then print "Merit" end if end if
Q4
if age >= 12 and tall = true then print "Ride" end if
Q5
if raining = true then if windy = true then print "Coat" end if end if
Section E: Spot the Error
Q1
B — The inner if is missing the keyword then. It should be: if has_ticket = true then
Q2
B — The inner if ends with the wrong word. The keyword is then, not than. It should be: if score >= 90 then
Q3
C — The comparison operator => is not valid. 'Greater than' is > (or >= for 'greater than or equal to'). It should be: if humidity > 70 then
Q4
C — say is not a pseudocode keyword. Output is written with print. It should be: print "Admin panel"
Q5
D — The text being printed is missing its closing quote. It should be: print "Very hot"