Closing nested structures in the right order

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

Closing nested structures in the right order

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. An IF sits inside a REPEAT. Which end line comes first?
A END IF, then END REPEAT
B END REPEAT, then END IF
2. Which rule is correct for pseudocode blocks?
A Each IF needs its own END IF
B One END IF can close several IF lines
3. A REPEAT 5 TIMES loop has no END REPEAT. What is this?
A An error, because the loop is never closed
B Fine, because the loop ends by itself
4. Which pseudocode closes its blocks correctly? REPEAT 3 TIMES IF score > 5 THEN SAY "Win"
A Add END REPEAT, then END IF
B Add END IF, then END REPEAT
5. Which line does this END IF match? REPEAT UNTIL lives = 0 IF hit = true THEN lives ← lives - 1 END IF END REPEAT
A It matches REPEAT UNTIL lives = 0
B It matches IF hit = true THEN

Match the Pairs

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

Left columnRight column
1REPEAT 5 TIMES IF score > 10 THEN SAY "Win" END IF END REPEAT AThe END IF is missing. The IF block is never closed, which is an error. Add END IF before END REPEAT.
2END REPEAT BThe IF is inside the loop, so END IF closes first and END REPEAT closes last.
3Each IF needs its own END IF, even when the IF sits inside a loop. CThe end lines are in the wrong order. The innermost block (IF) must be closed first, so END IF goes before END REPEAT.
4Close the innermost structure first, then the outer one, so the end lines match their opening lines like a stack. DCloses the REPEAT loop that was opened earlier. It must match a REPEAT line and come after any inner END IF.
5REPEAT 4 TIMES IF x > 5 THEN SAY "Big" END REPEAT EREPEAT UNTIL lives = 0 IF hit = true THEN lives ← lives - 1 END IF END REPEAT
6REPEAT 4 TIMES IF x > 5 THEN SAY "Big" END REPEAT END IF FREPEAT 3 TIMES IF age >= 13 THEN SAY "Teen" END IF END REPEAT

Definitions

END IF / END REPEAT

Fill in the Blanks

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

1.
SET score TO score + 1 ______ END REPEAT
2.
SET score TO score + 1 END IF __________
3.
IF lives = 0 THEN SAY "Game over" ______
4.
______ 3 TIMES SAY "Hi" END REPEAT
5.
SAY "Done" END IF END ______

Spot the Error

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

Question 1
A SET total TO 0
B IF total < 10 THEN
C IF total < 10 THEN
D ADD 2 TO total
E END IF
F END REPEAT
Question 2
A SET n TO 0
B REPEAT 3 TIMES
C REPEAT 2 TIMES
D SAY "Low"
E END IF
F END REPEAT
Question 3
A REPEAT 5 TIMES
B IF key = "w" THEN
C SAY "Go"
D END REPEAT
E END REPEAT
Question 4
A REPEAT 2 TIMES
B REPEAT 3 TIMES
C SAY "Welcome"
D END REPEAT
E END IF
Question 5
A SET lives TO 3
B IF lives > 0 THEN
C IF lives > 1 THEN
D SAY "Go"
E END REPEAT
F END IF

Answer Key

Closing nested structures in the right order
Section A: Quick Questions
Q1
A — END IF, then END REPEAT
Q2
A — Each IF needs its own END IF
Q3
A — An error, because the loop is never closed
Q4
B — Add END IF, then END REPEAT
Q5
B — It matches IF hit = true THEN
Section B: Match the Pairs
Q1
REPEAT 5 TIMES IF score > 10 THEN SAY "Win" END IF END REPEAT → The IF is inside the loop, so END IF closes first and END REPEAT closes last.
Q2
END REPEAT → Closes the REPEAT loop that was opened earlier. It must match a REPEAT line and come after any inner END IF.
Q3
Each IF needs its own END IF, even when the IF sits inside a loop. → REPEAT 3 TIMES IF age >= 13 THEN SAY "Teen" END IF END REPEAT
Q4
Close the innermost structure first, then the outer one, so the end lines match their opening lines like a stack. → REPEAT UNTIL lives = 0 IF hit = true THEN lives ← lives - 1 END IF END REPEAT
Q5
REPEAT 4 TIMES IF x > 5 THEN SAY "Big" END REPEAT → The END IF is missing. The IF block is never closed, which is an error. Add END IF before END REPEAT.
Q6
REPEAT 4 TIMES IF x > 5 THEN SAY "Big" END REPEAT END IF → The end lines are in the wrong order. The innermost block (IF) must be closed first, so END IF goes before END REPEAT.
Section C: Definitions
Q1
END IF / END REPEAT — Each IF and each REPEAT needs its own end line. Close the innermost block first, so END IF comes before END REPEAT when an IF is inside a loop. A missing end line is an error.
Section D: Fill in the Blanks
Q1
SET score TO score + 1 END IF END REPEAT
Q2
SET score TO score + 1 END IF END REPEAT
Q3
IF lives = 0 THEN SAY "Game over" END IF
Q4
REPEAT 3 TIMES SAY "Hi" END REPEAT
Q5
SAY "Done" END IF END REPEAT
Section E: Spot the Error
Q1
B — Line 2 opens a second IF, but the outer block is closed by END REPEAT. It should be REPEAT 4 TIMES so that END REPEAT has a REPEAT to match.
Q2
C — Line 3 opens a REPEAT, but it is closed by END IF, so the end line does not match its opening line. It should be IF n < 2 THEN.
Q3
D — The innermost block must be closed first. The IF is still open on line 4, so this line should be END IF, and END REPEAT comes last on line 5.
Q4
A — Line 1 opens a REPEAT, but the last line is END IF, so the outer end line has nothing to match. Line 1 should be IF hasTicket = 1 THEN.
Q5
C — Line 3 opens an IF, but it is closed by END REPEAT. The inner block should be REPEAT lives TIMES so the END REPEAT matches it.