Branching inside a loop

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

Branching inside a loop

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. A student writes an IF inside a REPEAT. How many times is the IF checked?
A Every time the loop repeats
B Only once, on the first repeat
2. Which algorithm shows an IF inside a REPEAT?
A IF the light is red THEN stop REPEAT 5 times walk forward
B REPEAT 5 times IF the light is red THEN stop
3. What does the indenting tell you in this algorithm? REPEAT 4 times IF the cup is empty THEN fill it
A The IF is inside the loop
B The IF happens after the loop ends
4. A robot does this: REPEAT 3 times, IF the floor is dirty THEN clean it. The floor is dirty on pass 1, clean on pass 2, dirty on pass 3. What happens?
A It cleans on pass 1 and pass 3 only
B It cleans on all 3 passes
5. Why can each pass through a loop with an IF inside it take a different path?
A The loop writes a new algorithm each time
B The condition is checked again each time and might change

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 tired THEN rest ANothing is indented, so it is unclear that the IF is inside the REPEAT. Indent the IF and the step under it.
2REPEAT UNTIL door is open IF door is locked THEN use key BThe IF is indented inside the REPEAT, so the check happens on every one of the 5 repetitions
3Each pass through the loop can take a different path because the IF is checked every time CREPEAT UNTIL bag is full IF fruit is ripe THEN pick fruit
4A loop that stops when a decision is met DThe loop keeps going until the door opens, and each pass decides whether the key is needed
5REPEAT 3 TIMES IF ball is red THEN throw ball EThe REPEAT is inside the IF, so the hungry check happens only once. The IF should be inside the REPEAT to be checked each time.
6IF dog is hungry THEN REPEAT 3 TIMES feed dog FREPEAT 4 TIMES IF light is red THEN stop ELSE walk

Definitions

IF inside REPEAT

Fill in the Blanks

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

1.
REPEAT 5 TIMES __ light is red THEN stop
2.
REPEAT 7 TIMES IF door is open ____ walk through
3.
REPEAT _____ bag is full IF fruit is an apple THEN add it to bag
4.
REPEAT UNTIL seat found IF seat is free THEN sit down ____ walk on
5.
REPEAT 10 TIMES IF score _ 5 THEN add 1 to points

Spot the Error

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

Question 1
A REPEAT 5 TIMES
B IF the light is red THEN
C drive forward
D ELSE
E drive forward
Question 2
A REPEAT UNTIL the bag is full
B take out one apple
C IF the apple is bad THEN
D put it in the bin
Question 3
A REPEAT 10 TIMES
B IF the ball is hit
C add 1 to score
D END REPEAT
Question 4
A REPEAT 8 TIMES
B move forward
C REPEAT there is a wall THEN
D turn right
Question 5
A SET count TO 0
B REPEAT 6 TIMES
C IF the card is a star THEN
D SET count TO 1
E SAY count

Answer Key

Branching inside a loop
Section A: Quick Questions
Q1
A — Every time the loop repeats
Q2
B — REPEAT 5 times IF the light is red THEN stop
Q3
A — The IF is inside the loop
Q4
A — It cleans on pass 1 and pass 3 only
Q5
B — The condition is checked again each time and might change
Section B: Match the Pairs
Q1
REPEAT 5 TIMES IF tired THEN rest → The IF is indented inside the REPEAT, so the check happens on every one of the 5 repetitions
Q2
REPEAT UNTIL door is open IF door is locked THEN use key → The loop keeps going until the door opens, and each pass decides whether the key is needed
Q3
Each pass through the loop can take a different path because the IF is checked every time → REPEAT 4 TIMES IF light is red THEN stop ELSE walk
Q4
A loop that stops when a decision is met → REPEAT UNTIL bag is full IF fruit is ripe THEN pick fruit
Q5
REPEAT 3 TIMES IF ball is red THEN throw ball → Nothing is indented, so it is unclear that the IF is inside the REPEAT. Indent the IF and the step under it.
Q6
IF dog is hungry THEN REPEAT 3 TIMES feed dog → The REPEAT is inside the IF, so the hungry check happens only once. The IF should be inside the REPEAT to be checked each time.
Section C: Definitions
Q1
IF inside REPEAT — An IF placed inside a REPEAT is checked again on every repetition, so each pass through the loop can take a different path.
Section D: Fill in the Blanks
Q1
REPEAT 5 TIMES IF light is red THEN stop
Q2
REPEAT 7 TIMES IF door is open THEN walk through
Q3
REPEAT UNTIL bag is full IF fruit is an apple THEN add it to bag
Q4
REPEAT UNTIL seat found IF seat is free THEN sit down ELSE walk on
Q5
REPEAT 10 TIMES IF score > 5 THEN add 1 to points
Section E: Spot the Error
Q1
C — Now both paths do the same thing, so the IF makes no difference. When the light is red the car should stop. The line should be: stop the car.
Q2
A — We take apples out, so the bag gets emptier, not fuller. The loop would never stop. It should be: REPEAT UNTIL the bag is empty.
Q3
B — An IF needs the word THEN at the end of the condition line. It should be: IF the ball is hit THEN.
Q4
C — A decision inside a loop needs the word IF, not REPEAT. The robot should check IF there is a wall THEN on every repeat.
Q5
D — This resets count to 1 every time a star is found, so it can never go higher than 1. It should add to the count: ADD 1 TO count.