Three levels of nesting

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

Three levels of nesting

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. A FOR loop contains an IF, and that IF contains another IF. Which closing order is correct?
A ENDIF, ENDIF, then NEXT
B NEXT, ENDIF, then ENDIF
2. Which statement about indentation in a three-level nested algorithm is true?
A All three levels are written at the same indentation
B Each level is indented one step more than the structure around it
3. A WHILE loop contains a FOR loop, and the FOR loop contains an IF. Which structure is closed first?
A ENDIF
B ENDWHILE
4. Which shows the correct closing order for WHILE containing FOR containing IF?
A ENDWHILE, NEXT, ENDIF
B ENDIF, NEXT, ENDWHILE
5. How should you read a three-level nested algorithm to work out what it does?
A From the inside out, starting with the innermost structure
B Only the outermost structure matters, so ignore the rest

Match the Pairs

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

Left columnRight column
1ENDIF ENDIF NEXT i AOpens three levels of nesting: a WHILE containing a FOR containing an IF, each indented one level further than the one before.
2WHILE n > 0 DO FOR i <- 1 TO 3 IF i = n THEN BFOR i <- 1 TO 3 IF i > 1 THEN IF i < 3 THEN OUTPUT i
3A WHILE containing a FOR containing an IF must be closed in reverse order: ENDIF, then NEXT, then ENDWHILE. C ENDIF NEXT j ENDWHILE
4Indent each level of nesting one step further, so the innermost statement sits furthest to the right. DNEXT i closes the FOR before the outer IF has been closed. The structures overlap instead of nesting. The outer ENDIF must come before NEXT i.
5FOR i <- 1 TO 5 IF a > 0 THEN IF b > 0 THEN OUTPUT i ENDIF NEXT i ENDIF EThe outermost WHILE loop is never closed. ENDWHILE is missing after NEXT i.
6WHILE n > 0 DO FOR i <- 1 TO 3 IF i = n THEN OUTPUT i ENDIF NEXT i FCloses the inner IF first, then the outer IF, then the FOR loop. This is the reverse of the order they were opened.

Definitions

nesting

Fill in the Blanks

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

1.
FOR Day <- 1 TO 7 IF Temp > 20 THEN IF Rain = 0 ____
2.
ENDIF NEXT Count ________
3.
WHILE Total < 100 __ FOR Count <- 1 TO 5 IF Mark > 50 THEN
4.
WHILE Total < 100 DO ___ Count <- 1 TO 5 IF Mark > 50 THEN
5.
ENDIF ENDIF ____ Day

Spot the Error

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

Question 1
A FOR Day <- 1 TO 7
B IF Rain > 10
C IF Temp < 15 THEN
D ENDIF
E ENDIF
F NEXT Day
Question 2
A WHILE Total < 100 THEN
B FOR N <- 1 TO 5
C IF N MOD 2 = 0 THEN
D ENDIF
E NEXT N
F ENDWHILE
Question 3
A FOR S <- 1 TO 30
B IF Mark[S] >= 50 THEN
C IF Mark[S] >= 80 THEN
D ENDWHILE
E ENDIF
F NEXT S
Question 4
A WHILE Lives > 0 DO
B FOR Shot <- 1 UNTIL 3
C IF Hit = TRUE THEN
D ENDIF
E NEXT Shot
F ENDWHILE
Question 5
A FOR Seat <- 1 TO 40
B IF Seat < 21 THEN
C IF Seat MOD 2 == 0 THEN
D ENDIF
E ENDIF
F NEXT Seat

Answer Key

Three levels of nesting
Section A: Quick Questions
Q1
A — ENDIF, ENDIF, then NEXT
Q2
B — Each level is indented one step more than the structure around it
Q3
A — ENDIF
Q4
B — ENDIF, NEXT, ENDWHILE
Q5
A — From the inside out, starting with the innermost structure
Section B: Match the Pairs
Q1
ENDIF ENDIF NEXT i → Closes the inner IF first, then the outer IF, then the FOR loop. This is the reverse of the order they were opened.
Q2
WHILE n > 0 DO FOR i <- 1 TO 3 IF i = n THEN → Opens three levels of nesting: a WHILE containing a FOR containing an IF, each indented one level further than the one before.
Q3
A WHILE containing a FOR containing an IF must be closed in reverse order: ENDIF, then NEXT, then ENDWHILE. → ENDIF NEXT j ENDWHILE
Q4
Indent each level of nesting one step further, so the innermost statement sits furthest to the right. → FOR i <- 1 TO 3 IF i > 1 THEN IF i < 3 THEN OUTPUT i
Q5
FOR i <- 1 TO 5 IF a > 0 THEN IF b > 0 THEN OUTPUT i ENDIF NEXT i ENDIF → NEXT i closes the FOR before the outer IF has been closed. The structures overlap instead of nesting. The outer ENDIF must come before NEXT i.
Q6
WHILE n > 0 DO FOR i <- 1 TO 3 IF i = n THEN OUTPUT i ENDIF NEXT i → The outermost WHILE loop is never closed. ENDWHILE is missing after NEXT i.
Section C: Definitions
Q1
nesting — Putting one structure inside another, three levels deep. Indent each level and close them in reverse order: innermost first, e.g. ENDIF, ENDIF, then NEXT.
Section D: Fill in the Blanks
Q1
FOR Day <- 1 TO 7 IF Temp > 20 THEN IF Rain = 0 THEN
Q2
ENDIF NEXT Count ENDWHILE
Q3
WHILE Total < 100 DO FOR Count <- 1 TO 5 IF Mark > 50 THEN
Q4
WHILE Total < 100 DO FOR Count <- 1 TO 5 IF Mark > 50 THEN
Q5
ENDIF ENDIF NEXT Day
Section E: Spot the Error
Q1
B — The outer IF is missing THEN. Every IF condition must end with THEN, so the line should be IF Rain > 10 THEN.
Q2
A — A WHILE loop uses DO, not THEN. THEN belongs to IF. The line should be WHILE Total < 100 DO.
Q3
D — The innermost structure is an IF, so it must be closed first with ENDIF. ENDWHILE belongs to a WHILE loop, and there is none here.
Q4
B — A FOR loop gives its end value with TO, not UNTIL. UNTIL belongs to REPEAT. The line should be FOR Shot <- 1 TO 3.
Q5
C — Cambridge pseudocode tests equality with a single =, not ==. The line should be IF Seat MOD 2 = 0 THEN.