Repeat: loops
Be able to use 'repeat N times' instead of writing the same thing multiple times.
Prerequisites
- AControlling a process with commandsrequired
Everyday explanation
send email, send email, send email, send email, send email — the same thing five times. Tedious to write, easy to miscount. Instead: repeat 5 times: send email.
This is called a loop. The computer does what is inside, over and over, as many times as you specify.
Inside the loop, there can be several commands: repeat 4 times: send email, mark as read — the system handles four items!
Interactive
Count without running:
repeat 3 times:
send email
send email
How many emails are sent? 3 · 2 = 6.
repeat 4 times:
send email
mark as read
What happens? Four items are handled: one is sent, one is marked, and so on. Each iteration performs both steps.
Rewrite «send, send, send, archive, send, send, send, archive» using loops.
Mastery means
- Replaces repeated commands with 'repeat N times'
- Calculates what a loop does without running it
Sign in to do the exercises and build your mastery up.
Sources
- CS Unplugged (CC BY-SA 4.0) — CC BY-SA 4.0
- Scratch Wiki (CC BY-SA 4.0) — CC BY-SA 4.0