GoalExplain the difference between a count-controlled loop and a condition-controlled loop and read a program that uses repeat until.
New words
iteration · repeating steps, also called loopingcount-controlled loop · a loop that repeats a set number of timescondition · something that is either true or false, such as “the s key is pressed”condition-controlled loop · a loop that repeats until a condition becomes true
Explanation
Repeating steps is called iteration. A count-controlled loop repeats a set number of times, such as repeat (4). A condition-controlled loop repeats until something becomes true. In Scratch the repeat until block is in the Control group. Scratch checks the condition at the start of every round. While it is false, the blocks inside run again. As soon as it is true, the loop ends and the program carries on with the block underneath. A forever loop has no condition, so only the stop button ends it.
Worked examples
Check
Is s pressed?
What happens
1
no
play the tambourine and rest
2
no
play the tambourine and rest
3
yes
the loop ends and the sprite says Done!
Here the player presses the s key during the second round and holds it. Scratch checks the condition at the start of each round. The tambourine plays 2 times, not 3.
People use condition-controlled loops too. A guitar player turns the tuning peg a little, plucks the string and listens. This repeats while the note is too low. When the note is right, the player stops.
Loop
Stops when
Example
repeat (4)
4 rounds are done
play a phrase 4 times
repeat until <key pressed?>
the key is pressed
play a drum until s is pressed
forever
someone clicks stop
background music
Three kinds of loop and how each one stops.
Class activity
Repeat until (with a friend): you are the program. Clap once and pat your knees once, again and again. Your friend is the player. They raise a hand at any time. You may only look at your friend’s hand at the start of each round. When you see a raised hand, you stop and say “Done!”. How many rounds did you play? Then try a count-controlled version: clap and pat exactly 5 times. Which version needs you to count?
Practice
1
Which kind of loop is each one? A count-controlled, B condition-controlled or C a forever loop.
A is count-controlled, B is condition-controlled and C is a forever loop that only the stop button ends.
2
The player presses the up arrow key during round 3 and holds it. How many notes are played in all?
6 notes. Rounds 1, 2 and 3 each play 2 notes. The condition is checked at the start of round 4, it is true, and the loop ends.
3
Give two examples from everyday life of repeating something until it is true. Do not use music.
For example: knocking on a door until somebody opens it, or walking until you reach the school gate.
4
Choose the block that makes the drum stop when the d key is pressed.
The key [d v] pressed? block from the Sensing group goes into the empty slot of repeat until. Then the drum plays again and again until the d key is pressed.