Page 91 - 2502_Pakistan-kifayat_C-8
P. 91
When the green flag is clicked, the sprite starts moving forward by 10 steps repeatedly until it touches the
edge of the stage. This is controlled by a repeat until block, which stops once the sprite reaches the edge.
After that, a forever block begins, which continuously changes the sprite’s colour effect by 25 every 0.2
seconds. This block keeps running without stopping, so the sprite continues changing colours endlessly.
Nesting Loops
Nesting means putting one thing inside another. Nesting loops means putting one loop block inside
another loop block in a script. In Scratch, this helps you repeat actions as part of a bigger repeating task.
For example, you might want a sprite to move 5 steps and do that 3 times, then repeat the whole thing
4 times. Nesting loops help you do this without writing the same instructions again and again.
Imagine your teacher gives you a task: wish “Good morning” to every teacher 3 times. That is the outer
loop. Each time you greet a teacher, you also clap 5 times. That is the inner loop. So, you clap 5 times for
each greeting. Since you greet 3 teachers, you clap a total of 15 times and say “Good morning” 3 times.
To move sprite in pattern with a rotating effect, use the following script:
When the green flag is clicked, the sprite will start moving. It will repeat a set of actions four times. Inside
this, there is another inner loop that also repeats four times. During each repeat of the inner loop, the
sprite moves forward by 50 steps, waits for half a second, then turns 90 degrees to the right. It waits again
for half a second and then turns a smaller angle of 15 degrees to the right. Because the loops are nested,
the sprite will perform these movements many times, creating a pattern with both large and small turns.
This will make the sprite move in a shape that changes direction slowly.
Another example of a nested loop would be this. You have to create a script in Scratch that makes a sprite
jump 5 times, then wait until the space key is pressed before jumping again. The repeat loop makes the
sprite jump 5 times, and the forever loop keeps repeating this whole process. Inside the forever loop,
you can add a wait until block that pauses the script until the space key is pressed before the repeat loop
starts again.
#Intermediate Level Programming 89

