AdvertisementYour brand could be hereYour brand hereOn every Darslab page · students, parents and teachersinfo@darslab.comAsk for pricesPricesinfo@darslab.com
☰ Year plan · Computing

Music that repeats

Week 14 · Unit 6: Be a musician · 3 lessons
3

Repeat until a key is pressed

Computing 4: Unit 6, pp. 74–89
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.

when this sprite clickedplay drum(7) Tambourinefor0.25beatsrest for0.25beatsrepeat untilkeyspressed?sayDone!for2seconds
Worked examples
CheckIs s pressed?What happens
1noplay the tambourine and rest
2noplay the tambourine and rest
3yesthe 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.
Startnote too low?yesturn the peg a littlepluck and listennoEnd
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.
LoopStops whenExample
repeat (4)4 rounds are doneplay a phrase 4 times
repeat until <key pressed?>the key is pressedplay a drum until s is pressed
foreversomeone clicks stopbackground 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.
Aplay note60for0.25beatsrepeat8Bplay note60for0.25beatsrepeat untilkeyspacepressed?Cplay note60for0.25beatsforever
2
The player presses the up arrow key during round 3 and holds it. How many notes are played in all?
whenclickedplay note72for0.5beatsplay note76for0.5beatsrepeat untilkeyup arrowpressed?
3
Give two examples from everyday life of repeating something until it is true. Do not use music.
4
Choose the block that makes the drum stop when the d key is pressed.
when this sprite clickedplay drum(1) Snare Drumfor0.25beatsrepeat until