Introduction to 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.
➔ 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.
● 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 -
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.
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.
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.