Scratch with Microbit : Nested Loop

Page 1

Introduction to Nested Loop

Nested Loop

A nested loop is a loop within a loop, an inner loop within the body of an outer one. How this works is that the first pass of the outer loop triggers the inner loop, which executes to completion. Then the second pass of the outer loop triggers the inner loop again. This repeats until the outer loop finishes.

Use of Nested Loop

➔ We can make use of nested repeat block to show any message.

➔ Let’s see one example how to implement a nested loop from scratch.

In this given script we have used two different repeat loops. 1 inner loop and 1 outer loop to say some text as specified. The inner repeat loop will work till the number specified inside the outer loop which is 4.

Use of Nested Loop in Drawing pattern

● We can use the nested repeat to draw amazing pattern in scratch.

● Let’s see how it works.

● To draw such spiral pattern it requires more than one loops. Let’s code for this -

Code for Drawing Spiral:

In this Script, we have used two different repeat loop. The inner repeat will draw the square and with the help of outer repeat the inner loop will repeat 90 times to draw the next square so that it can look like a spiral. Follow this given script to draw your spiral drawing.

Think of making some changes to draw something more amazing.

Drawing Pattern with Pentagon ➔ To draw a pentagon ➔ Total Number of Sides = 5 ➔ Turn Angle = 360/ 5 = 72 Degree With the help of given script draw a pentagon.

Drawing Pattern with Pentagon

In the last project we just made a pentagon using repeat. Now let’s draw some creative pattern using nested repeat and drawing a pentagon.

Here using the previous code and nested repeat we have created this pattern. Let’s code for this.

Code for the Pattern

In this script when you click on the button ‘A’ in micro bit it will draw the required pattern.

To erase the pattern and reset your sprite shake your microbit.

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.