Scratch with Microbit : Loop

Page 1

Introduction to Loop

What is Loops?

If we wanted to repeat a certain action in code multiple times, it would be best and more efficient to use a loop, that way the code is easier to read and there is less to write. Loops are great tools to use within code and projects to repeat an action multiple times.

Loops in Scratch

In the 'Control' drawer, there are three types of loops: repeat x number of times, repeat until, and forever. Each type of loop has a different purpose and knowing what they do is important in writing code!

Repeat-until Loop

Sometimes, we don’t necessarily know ahead of time how many times we should repeat a block in our code. In these situations, we want to use the repeat until block. Any code that is put inside of the repeat until block will continue to happen until the condition is true.

For example, we could make a character move forward 10 steps in whatever direction it is facing until it is touching the edge of the screen.

Repeat-until with variables

Print the value of variable “X” until it terminates the condition inside the repeat until block.

1. Create a variable ‘X’.

2. Go to the control tab and find repeat until block.

3. Now we have to specify the condition, Go to the operators and drag and drop the greater than operator block ( Value1 > Value2 ).

4. Next step is to specify the value upto which you want to print the value of variable ‘X’.

5. Inside repeat until you place the say block to show the value of ‘X’.

6. In this step we need to increment the value of variable ‘X’ for every iteration of the loop.

Repeat Loop

Some loops need to repeat a specific number of times. To create a loop like this in Scratch, you would need to use the repeat (number) block. This allows you to repeat a sequence of commands a particular number of times; you set the number of times the loop will repeat at the top of the block. In programming, this type of loop is called a ‘count-controlled’ loop.

Note: When you use a repeat (number) loop you can add code after it. After the loop has run for the given number of times, then the code after the loop will run.

Adding block to a repeat

For example we want to move a sprite multiple times so instead of using multiple move steps we can simply add a repeat block and specify the number up to which we want to move the sprite.

Note: Both Script can be used to move the sprite 10 steps 5 times but using repeat it becomes very easy as it reduces the lines of code.

Drawing A square shape using repeat

To draw a square in scratch we need to follow these steps as given below:

1. Go to Microbit Extension and drag and drop the when ‘A’ button is pressed.

2. Go to the extension and add the pen extension to draw any shape.

3. Get the pen down block from pen extension and connect it after the flag clicked.

4. Now to draw a square we need to repeat some steps so go to control tab and draw and drop the repeat block.

5. Inside this repeat we have to mention the value to steps so in this case we required 4 so mention 4 in it.

6. Now it’s time to place the motion blocks so go to the motion tab and drag and drop move steps and turn right blocks and place it inside the repeat.

7. In the last we just need to add a wait so that we can see the shape drawing clearly. After this step click on the flag to see the output as a square.

Forever Loop

It is a control block. This iteration block executes a particular set of steps infinitely. There is no stopping condition present in forever block. In Scratch, this type of block is used to make patterns that are infinite in nature. For example, the square spiral.

Drawing a square spiral using forever block:
1. Click on Add Extension and add microbit to start the program when Button ‘A’ Pressed.(drag it to center). 2. Click on add extension and choose pen block for drawing a square spiral. 3. Place pen down the block below the event block. 4. Go to the variable block and drag “set my variable to 80”.

5. Drag the forever block from the control block and place it down of pen down block.

6. Click on motion block and drag move block and place it inside the repeat block. Now change the number to “my variable”.

7. Click on turn block (inside motion block) and place it below the move block inside the repeat block. Now, change the number from 15 to 90.

8. Go to the variable block and drag “change my variable by 5”.

9. Now run the program.

Turn static files into dynamic content formats.

Create a flipbook
Issuu converts static files into: digital portfolios, online yearbooks, online catalogs, digital photo albums and more. Sign up and create your flipbook.