Iteration inside selection

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

Iteration inside selection

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. IF Score > 50 THEN FOR i <- 1 TO 3 OUTPUT "Pass" NEXT i ENDIF. Score is 40. What happens?
A The FOR loop is skipped and nothing is output
B "Pass" is output 3 times
2. Which is the correct order to close a FOR loop inside an IF?
A ENDIF, then NEXT i
B NEXT i, then ENDIF
3. IF Age >= 18 THEN FOR i <- 1 TO 2 OUTPUT i NEXT i ENDIF. Age is 20. What is output?
A Nothing is output
B 1 then 2
4. A WHILE loop is inside the THEN branch of an IF. Where does ENDIF go?
A After ENDWHILE
B Before ENDWHILE
5. A FOR loop is written inside the ELSE branch. When does it run?
A Only when the IF condition is FALSE
B Every time the IF statement is reached

Match the Pairs

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

Left columnRight column
1IF Age >= 18 THEN FOR I <- 1 TO 3 OUTPUT I NEXT I ENDIF AIF Go = TRUE THEN FOR N <- 1 TO 5 OUTPUT N NEXT N ENDIF
2IF Score > 50 THEN OUTPUT "Pass" ELSE WHILE Score < 50 DO Score <- Score + 10 ENDWHILE ENDIF BIF Total > 100 THEN FOR I <- 1 TO 3 OUTPUT "Big" NEXT I ELSE OUTPUT "Small" ENDIF
3The ENDIF comes after NEXT, because the loop is closed first and then the IF is closed CENDIF is placed before NEXT I. The loop is inside the IF, so NEXT I must come first and ENDIF after it.
4If the condition is FALSE, the loop in the THEN branch is skipped and the ELSE part runs instead DThe FOR loop runs three times only when Age is 18 or more. Otherwise the whole loop is skipped.
5IF X > 0 THEN FOR I <- 1 TO 3 OUTPUT I ENDIF NEXT I EThe WHILE loop is inside the ELSE branch, so it only runs when Score > 50 is FALSE.
6IF Ready = TRUE THEN WHILE Count < 5 DO Count <- Count + 1 ENDIF ENDWHILE FThe WHILE loop is closed with ENDIF and the IF is closed with ENDWHILE, so the end keywords are the wrong way round.

Definitions

IF...THEN FOR...NEXT...ENDIF

Fill in the Blanks

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

1.
IF Age >= 18 THEN ___ Count <- 1 TO 3 OUTPUT "Welcome"
2.
OUTPUT Count NEXT Count _____
3.
ELSE _____ Tries < 3 DO Tries <- Tries + 1
4.
N <- N - 1 ________ ENDIF
5.
CASE OF Choice 1 : FOR Count <- 1 __ 5 OUTPUT Count

Spot the Error

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

Question 1
A IF Mark >= 50 THEN
B FOR i <- 1 TO 3
C PRINT i
D NEXT i
E ENDIF
Question 2
A IF Lives > 0 THEN
B FOR i <- 1 TO Lives
C OUTPUT i
D ENDFOR
E ENDIF
Question 3
A IF Total > 100
B WHILE Total > 100 DO
C Total <- Total - 10
D ENDWHILE
E ENDIF
Question 4
A IF Answer = "Y" THEN
B FOR Count = 1 TO 5
C OUTPUT "Hello"
D NEXT Count
E ENDIF
Question 5
A CASE OF Choice
B 1 : FOR i <- 1 TO 3
C OUTPUT i
D ENDIF
E ENDCASE

Answer Key

Iteration inside selection
Section A: Quick Questions
Q1
A — The FOR loop is skipped and nothing is output
Q2
B — NEXT i, then ENDIF
Q3
B — 1 then 2
Q4
A — After ENDWHILE
Q5
A — Only when the IF condition is FALSE
Section B: Match the Pairs
Q1
IF Age >= 18 THEN FOR I <- 1 TO 3 OUTPUT I NEXT I ENDIF → The FOR loop runs three times only when Age is 18 or more. Otherwise the whole loop is skipped.
Q2
IF Score > 50 THEN OUTPUT "Pass" ELSE WHILE Score < 50 DO Score <- Score + 10 ENDWHILE ENDIF → The WHILE loop is inside the ELSE branch, so it only runs when Score > 50 is FALSE.
Q3
The ENDIF comes after NEXT, because the loop is closed first and then the IF is closed → IF Go = TRUE THEN FOR N <- 1 TO 5 OUTPUT N NEXT N ENDIF
Q4
If the condition is FALSE, the loop in the THEN branch is skipped and the ELSE part runs instead → IF Total > 100 THEN FOR I <- 1 TO 3 OUTPUT "Big" NEXT I ELSE OUTPUT "Small" ENDIF
Q5
IF X > 0 THEN FOR I <- 1 TO 3 OUTPUT I ENDIF NEXT I → ENDIF is placed before NEXT I. The loop is inside the IF, so NEXT I must come first and ENDIF after it.
Q6
IF Ready = TRUE THEN WHILE Count < 5 DO Count <- Count + 1 ENDIF ENDWHILE → The WHILE loop is closed with ENDIF and the IF is closed with ENDWHILE, so the end keywords are the wrong way round.
Section C: Definitions
Q1
IF...THEN FOR...NEXT...ENDIF — A loop written inside an IF branch only runs when that branch is chosen. If the condition is FALSE, it is skipped. NEXT ends the loop first, then ENDIF ends the IF.
Section D: Fill in the Blanks
Q1
IF Age >= 18 THEN FOR Count <- 1 TO 3 OUTPUT "Welcome"
Q2
OUTPUT Count NEXT Count ENDIF
Q3
ELSE WHILE Tries < 3 DO Tries <- Tries + 1
Q4
N <- N - 1 ENDWHILE ENDIF
Q5
CASE OF Choice 1 : FOR Count <- 1 TO 5 OUTPUT Count
Section E: Spot the Error
Q1
C — PRINT is not a Cambridge pseudocode keyword. Inside the loop, which only runs when Mark >= 50, the statement should be OUTPUT i.
Q2
D — Cambridge pseudocode ends a FOR loop with NEXT and the counter, not ENDFOR. It should be NEXT i, and ENDIF then closes the IF.
Q3
A — The IF line is missing THEN. It should be IF Total > 100 THEN, so the WHILE loop inside only runs when the condition is TRUE.
Q4
B — A FOR loop sets its counter with the assignment arrow <-, not =. It should be FOR Count <- 1 TO 5.
Q5
D — The FOR loop inside the CASE clause must be closed with NEXT i. ENDIF does not belong here, because ENDCASE closes the CASE.