Selection inside a loop (7c)

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

Selection inside a loop (7c)

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. A FOR loop runs 10 times and has an IF inside its body. How many times is the IF condition tested?
A 10 times, once on every pass of the loop
B Once, the first time the loop starts
2. Which pseudocode correctly counts the marks that are 50 or more?
A FOR Index ← 1 TO 10 IF Mark[Index] >= 50 THEN Count ← Count + 1 NEXT Index ENDIF
B FOR Index ← 1 TO 10 IF Mark[Index] >= 50 THEN Count ← Count + 1 ENDIF NEXT Index
3. In a loop, where should the counting line Count ← Count + 1 go so that it only counts items meeting the condition?
A Outside the IF but inside the loop, so it runs on every pass
B Inside the IF, so it only runs when the condition is true
4. A WHILE loop reads numbers and has an IF inside it. What is true about the IF?
A It is tested again each time the loop body runs
B It is only tested once, before the loop starts
5. Which loop body counts how many Age values are over 17, if Count starts at 0?
A IF Age[Index] > 17 THEN Count ← Count + 1 ENDIF
B IF Age[Index] > 17 THEN Count ← 1 ENDIF

Match the Pairs

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

Left columnRight column
1FOR Count ← 1 TO 10 IF Count MOD 2 = 0 THEN OUTPUT Count ENDIF NEXT Count AFOR Index ← 1 TO 30 IF Mark[Index] >= 50 THEN Passes ← Passes + 1 ENDIF NEXT Index
2WHILE Number <> 0 DO IF Number > 50 THEN Total ← Total + 1 ENDIF ENDWHILE BOn each pass of the WHILE loop the IF checks the condition and adds 1 to the counter only when it is true
3To count how many marks are 50 or more, put an IF inside the loop that adds 1 to a counter when the condition is true CCount ← 0 is inside the loop, so the counter is reset on every pass and the final count is wrong. Set it to zero once before the loop.
4Each pass of the loop can take a different branch because the IF is tested again every time round DThe ENDIF is missing, so the IF inside the loop is not closed before NEXT. Add ENDIF after the THEN statement.
5FOR Index ← 1 TO 10 Count ← 0 IF Score[Index] > 80 THEN Count ← Count + 1 ENDIF NEXT Index EFOR Day ← 1 TO 7 IF Rain[Day] > 10 THEN OUTPUT "Wet" ELSE OUTPUT "Dry" ENDIF NEXT Day
6FOR Index ← 1 TO 10 IF Score[Index] > 80 THEN Count ← Count + 1 NEXT Index FThe IF is tested on every pass of the FOR loop, so only the even numbers are output

Definitions

IF inside a loop

Fill in the Blanks

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

1.
FOR Index ← 1 TO 10 __ Score[Index] > 50 THEN Passes ← Passes + 1
2.
FOR Index ← 1 TO 10 IF Score[Index] > 50 THEN Passes ← ______ + 1
3.
WHILE Index <= 10 DO IF Temp[Index] > 30 ____ Hot ← Hot + 1
4.
FOR Num ← 1 TO 20 IF Num MOD 2 _ 0 THEN Evens ← Evens + 1
5.
FOR Index ← 1 TO 10 IF Mark[Index] >= 40 ___ Mark[Index] <= 60 THEN Middle ← Middle + 1

Spot the Error

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

Question 1
A Pass ← 0
B FOR Count = 1 TO 5
C IF Mark[Count] >= 50 THEN
D Pass ← Pass + 1
E ENDIF
F NEXT Count
Question 2
A Count ← 0
B FOR N ← 1 TO 10
C IF Score[N] > 80 THEN
D Count ← 1
E ENDIF
F NEXT N
Question 3
A WHILE Index <= 10 DO
B IF Temp[Index] < 30 THEN
C Hot ← Hot + 1
D ENDIF
E Index ← Index + 1
F ENDWHILE
Question 4
A Evens ← 0
B FOR Num ← 1 TO 20
C IF Num MOD 2 = 0 THEN
D Evens ← Evens + Num
E ENDIF
F NEXT Num
Question 5
A FOR Count ← 1 TO 10
B INPUT Age
C IF Age >= 18
D OUTPUT "Adult"
E ENDIF
F NEXT Count

Answer Key

Selection inside a loop (7c)
Section A: Quick Questions
Q1
A — 10 times, once on every pass of the loop
Q2
B — FOR Index ← 1 TO 10 IF Mark[Index] >= 50 THEN Count ← Count + 1 ENDIF NEXT Index
Q3
B — Inside the IF, so it only runs when the condition is true
Q4
A — It is tested again each time the loop body runs
Q5
A — IF Age[Index] > 17 THEN Count ← Count + 1 ENDIF
Section B: Match the Pairs
Q1
FOR Count ← 1 TO 10 IF Count MOD 2 = 0 THEN OUTPUT Count ENDIF NEXT Count → The IF is tested on every pass of the FOR loop, so only the even numbers are output
Q2
WHILE Number <> 0 DO IF Number > 50 THEN Total ← Total + 1 ENDIF ENDWHILE → On each pass of the WHILE loop the IF checks the condition and adds 1 to the counter only when it is true
Q3
To count how many marks are 50 or more, put an IF inside the loop that adds 1 to a counter when the condition is true → FOR Index ← 1 TO 30 IF Mark[Index] >= 50 THEN Passes ← Passes + 1 ENDIF NEXT Index
Q4
Each pass of the loop can take a different branch because the IF is tested again every time round → FOR Day ← 1 TO 7 IF Rain[Day] > 10 THEN OUTPUT "Wet" ELSE OUTPUT "Dry" ENDIF NEXT Day
Q5
FOR Index ← 1 TO 10 Count ← 0 IF Score[Index] > 80 THEN Count ← Count + 1 ENDIF NEXT Index → Count ← 0 is inside the loop, so the counter is reset on every pass and the final count is wrong. Set it to zero once before the loop.
Q6
FOR Index ← 1 TO 10 IF Score[Index] > 80 THEN Count ← Count + 1 NEXT Index → The ENDIF is missing, so the IF inside the loop is not closed before NEXT. Add ENDIF after the THEN statement.
Section C: Definitions
Q1
IF inside a loop — An IF placed inside a loop body is tested again on every pass, so each pass can take a different branch. It is often used to count items that meet a condition.
Section D: Fill in the Blanks
Q1
FOR Index ← 1 TO 10 IF Score[Index] > 50 THEN Passes ← Passes + 1
Q2
FOR Index ← 1 TO 10 IF Score[Index] > 50 THEN Passes ← Passes + 1
Q3
WHILE Index <= 10 DO IF Temp[Index] > 30 THEN Hot ← Hot + 1
Q4
FOR Num ← 1 TO 20 IF Num MOD 2 = 0 THEN Evens ← Evens + 1
Q5
FOR Index ← 1 TO 10 IF Mark[Index] >= 40 AND Mark[Index] <= 60 THEN Middle ← Middle + 1
Section E: Spot the Error
Q1
B — A FOR loop sets its counter with the assignment arrow ←, not =. The line should be FOR Count ← 1 TO 5.
Q2
D — Count is set back to 1 each time the IF is true, so it never builds up. To count items it must add 1 each time: Count ← Count + 1.
Q3
B — The program counts hot days, so the condition must test for temperatures above 30. The wrong operator < counts cold days. It should be IF Temp[Index] > 30 THEN.
Q4
D — Adding Num makes a total of the even numbers, not a count. Counting adds 1 each time the condition is true: Evens ← Evens + 1.
Q5
C — The IF line is missing the keyword THEN. It should be IF Age >= 18 THEN.