Using Loops in Scratch

Updated: February 17, 2022
No Credit Card Required


Oh, how are you? Have you come prepared to code? Good. Let's code.

Last lesson, we introduced two conditional statements. The first statement was the IF THEN then statement

IF it's raining outside, THEN take an umbrella.

We also learned the IF THEN ELSE statement.

IF it's raining outside, THEN take an umbrella, or ELSE wear a hat.

Today, I want to introduce a new function. REPEAT. This is also known as a LOOP. Loops are great when you need something to happen over and over again.

Let's say we want our sprite to move 10 steps in the direction it's facing. And then 10 steps back. Pretty easy to code right? Now, say you wanted your sprite to do that 5000 times. Okay, go ahead. Copy that 5000 times. “Oh, wait. Mom, I hate coding. This is so stupid”. Actually, what you want to do here is just use the REPEAT BLOCK and enter the number of times you want the computer to repeat the action.

Oh, there's also a REPEAT UNTIL BLOCK. REPEAT UNTIL is similar to REPEAT, but any code that we put inside the REPEAT UNTIL BLOCK gets repeated until a condition is true. In this example, here, the sprite keeps spinning until you press the spacebar. Here's an example where we move our sprite to a specific position using the REPEAT UNTIL BLOCK. THE REPEAT UNTIL BLOCK and REPEAT BLOCKS are examples of loops. They're used to repeat an action. So you do not have to copy a code snippet say 5000 times. That's too many.

Now what will happen if we put a REPEAT BLOCK inside another REPEAT BLOCK? We've created a loop inside of another loop. This is called a NESTED LOOP. Any idea what will happen? Well code it out in your editor and execute the code? How many times will it repeat the action?

Stop all is a command that ends the program. You can use this inside a loop to end the loop. Cool.

That ends today's lesson. I'll see you soon