Navigation

    CreatiCode Scratch Forum

    • Register
    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • CreatiCode

    Nested Loops

    Programming
    1
    1
    2021
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • info-creaticode
      CreatiCode last edited by admin

       

      Prerequisite

      • The For-Loop Block
      • The Repeat Block
      • The Repeat Until Block
      • The Forever Block

       

      What are Loops?

      A “loop” is a block for playing other blocks repeatedly. Below are some loop blocks you can use on the CreatiCode playground;


       

      What are Nested Loops?

      When you put one loop inside another loop, it becomes a “nested loop”. You can even have a third loop inside the inner loop.


       

      How Nested Loops Work?

      When you run a program with a nested loop, the inner (child) loop will work like a normal loop. However, because of the outer (parent) loop, the inner loop itself will be repeated.

      Here is a step-by-step example:

      Step 1: The main loop is played.


       

      Step 2: The Nested loop is started.


       

      Step 3: The blocks inside the nested loop are played until the condition of the nested loop is completed.


       

      Step 4: The blocks after the nested loop are played (if there are any).


       

      Step 5: The main loop is restarted.

      Note: All of the above 5 steps keep repeating until the condition of the main loop is completed.

      For example, as shown below, “repeat until ” is the nested loop, which keeps the monkey moving until it touches the edge of the stage. And “repeat (5)” is the main loop which repeats the nested loop 5 times.


       

      Use of Nested Loops

      We use Nested loops when we want to repeat multiple blocks at the same time, until multiple conditions are completed.

      For example, as shown below, nested loops are used to make the monkey move and eat bananas at the same time.


       

      1 Reply Last reply Reply Quote 1
      • First post
        Last post