Selection inside a loop (8.1b)

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

Selection inside a loop (8.1b)

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 checked?
A 10 times, once on every repeat of the loop
B Once, before the loop starts
2. Which code counts the even numbers from 1 to 20?
A FOR i <- 1 TO 20 IF i MOD 2 = 0 THEN Count <- Count + 1 ENDIF NEXT i
B FOR i <- 1 TO 20 IF i DIV 2 = 0 THEN Count <- Count + 1 ENDIF NEXT i
3. Which loop adds only the marks that are 50 or more to Total?
A Total <- Total + Mark outside the IF, after ENDIF
B Total <- Total + Mark inside an IF Mark >= 50 THEN ... ENDIF
4. Where must the ENDIF be placed when an IF is inside a FOR loop?
A Before NEXT, so the IF is closed inside the loop body
B After NEXT, so the IF wraps the whole loop
5. In a REPEAT...UNTIL loop, an IF inside the body checks the number just entered. When is the IF tested?
A Only on the first pass, then the result is reused
B On every pass, using the value entered on that pass

Match the Pairs

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

Left columnRight column
1FOR i <- 1 TO 10 IF i MOD 2 = 0 THEN Count <- Count + 1 ENDIF NEXT i ACounts the even numbers from 1 to 10. The IF is tested on every pass, so Count only goes up when i is even.
2WHILE Num <> -1 IF Num > 50 THEN Total <- Total + Num ENDIF INPUT Num ENDWHILE BThe loop and the selection overlap. NEXT i is inside the IF, so the ENDIF must come before NEXT i. A structure inside a loop must be fully closed before the loop ends.
3A selection inside a REPEAT...UNTIL loop is checked on every repeat, so each input can be handled differently, such as counting only passes. CThe IF is placed after NEXT, so it is outside the loop. It is only tested once, not on every pass. The IF must be moved inside the loop, before NEXT i.
4A CASE statement inside a FOR loop chooses a different branch on each pass, depending on the current value of the counter. DKeeps reading numbers until -1 is entered and adds only the numbers over 50 to Total.
5FOR i <- 1 TO 10 NEXT i IF i MOD 2 = 0 THEN Count <- Count + 1 ENDIF EREPEAT INPUT Mark IF Mark >= 50 THEN Passes <- Passes + 1 ENDIF UNTIL Mark = -1
6FOR i <- 1 TO 10 IF i MOD 2 = 0 THEN Total <- Total + i NEXT i ENDIF FFOR Day <- 1 TO 7 CASE OF Day 6, 7 : OUTPUT "Weekend" OTHERWISE : OUTPUT "Weekday" ENDCASE NEXT Day

Definitions

IF inside a loop

Fill in the Blanks

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

1.
FOR Num ← 1 TO 20 IF Num ___ 2 = 0 THEN Count ← Count + 1
2.
WHILE Count < 5 DO INPUT Mark IF Mark >= 50 ____
3.
REPEAT INPUT Num IF Num > 0 THEN Total ← Total + ___
4.
FOR Day ← 1 TO 7 CASE __ Day 1 : OUTPUT "Monday"
5.
FOR Num ← 1 TO 10 IF Num > 5 THEN Total ← Total + Num NEXT ___

Spot the Error

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

Question 1
A FOR Count ← 1 UNTIL 10
B IF Count MOD 2 = 0 THEN
C Evens ← Evens + 1
D ENDIF
E NEXT Count
Question 2
A Evens ← 0
B FOR N ← 1 TO 20 DO
C IF N MOD 2 = 0 THEN
D Evens ← Evens + 1
E ENDIF
F NEXT N
Question 3
A WHILE Num > 0 DO
B IF Num DIV 2 = 0 THEN
C Evens ← Evens + 1
D ENDIF
E Num ← Num - 1
F ENDWHILE
Question 4
A REPEAT
B INPUT Mark
C IF Mark >= 50
D Passes ← Passes + 1
E ENDIF
F UNTIL Mark = -1
Question 5
A FOR N ← 1 TO 5
B INPUT Num
C IF Num > 0 THEN
D Total ← Total + 1
E ENDIF
F NEXT N

Answer Key

Selection inside a loop (8.1b)
Section A: Quick Questions
Q1
A — 10 times, once on every repeat of the loop
Q2
A — FOR i <- 1 TO 20 IF i MOD 2 = 0 THEN Count <- Count + 1 ENDIF NEXT i
Q3
B — Total <- Total + Mark inside an IF Mark >= 50 THEN ... ENDIF
Q4
A — Before NEXT, so the IF is closed inside the loop body
Q5
B — On every pass, using the value entered on that pass
Section B: Match the Pairs
Q1
FOR i <- 1 TO 10 IF i MOD 2 = 0 THEN Count <- Count + 1 ENDIF NEXT i → Counts the even numbers from 1 to 10. The IF is tested on every pass, so Count only goes up when i is even.
Q2
WHILE Num <> -1 IF Num > 50 THEN Total <- Total + Num ENDIF INPUT Num ENDWHILE → Keeps reading numbers until -1 is entered and adds only the numbers over 50 to Total.
Q3
A selection inside a REPEAT...UNTIL loop is checked on every repeat, so each input can be handled differently, such as counting only passes. → REPEAT INPUT Mark IF Mark >= 50 THEN Passes <- Passes + 1 ENDIF UNTIL Mark = -1
Q4
A CASE statement inside a FOR loop chooses a different branch on each pass, depending on the current value of the counter. → FOR Day <- 1 TO 7 CASE OF Day 6, 7 : OUTPUT "Weekend" OTHERWISE : OUTPUT "Weekday" ENDCASE NEXT Day
Q5
FOR i <- 1 TO 10 NEXT i IF i MOD 2 = 0 THEN Count <- Count + 1 ENDIF → The IF is placed after NEXT, so it is outside the loop. It is only tested once, not on every pass. The IF must be moved inside the loop, before NEXT i.
Q6
FOR i <- 1 TO 10 IF i MOD 2 = 0 THEN Total <- Total + i NEXT i ENDIF → The loop and the selection overlap. NEXT i is inside the IF, so the ENDIF must come before NEXT i. A structure inside a loop must be fully closed before the loop ends.
Section C: Definitions
Q1
IF inside a loop — An IF or CASE placed in a loop body is tested again on every repeat, so each pass can take a different branch depending on the current value.
Section D: Fill in the Blanks
Q1
FOR Num ← 1 TO 20 IF Num MOD 2 = 0 THEN Count ← Count + 1
Q2
WHILE Count < 5 DO INPUT Mark IF Mark >= 50 THEN
Q3
REPEAT INPUT Num IF Num > 0 THEN Total ← Total + Num
Q4
FOR Day ← 1 TO 7 CASE OF Day 1 : OUTPUT "Monday"
Q5
FOR Num ← 1 TO 10 IF Num > 5 THEN Total ← Total + Num NEXT Num
Section E: Spot the Error
Q1
A — A count-controlled loop uses TO to set the end value, so it must be FOR Count ← 1 TO 10. UNTIL belongs to REPEAT…UNTIL.
Q2
B — A FOR loop does not use DO. DO belongs to WHILE loops. The line should be FOR N ← 1 TO 20.
Q3
B — DIV gives the whole-number quotient, not the remainder. To test for an even number on each pass, use MOD: IF Num MOD 2 = 0 THEN.
Q4
C — An IF statement needs THEN after its condition. The line should be IF Mark >= 50 THEN.
Q5
D — To total only the positive values, add the number entered on this pass. Adding 1 just counts them. It should be Total ← Total + Num.