Lesson 8: Loops

Lesson Description

Learn how to use loops to execute a block of code repetitively, based on a condition. Different types of loops, their implementation in PictoBlox, and their benefits are discussed.

Iteration

In programming, the repetition of a line or a block of code is also known as iteration. A loop is an algorithm that executes a block of code multiple times till the time a specified condition is met. Therefore, we can say that a loop iterates a block of code multiple times till the time-mentioned condition is satisfied.

Consider a requirement where we need to print numbers in incremental order from 1 to 1000. Although it is possible to print it with each line of code written, it will get a very tedious and lengthy process. This is where loops come into the picture to make this task easier. You can use the concept of loops and get the desired output by writing just a few lines of code.

Increment Loops

Loops provide the facility to execute a block of code repetitively, based on a condition. This block of code is executed repeatedly till the time-specified condition remains true. This condition is checked based on the loop’s control variable. Whenever this condition results in a false, the loop terminates. It is very important to keep this thing in mind while programming that the condition should result in false at a certain point in time. Otherwise, this block of code will enter an infinite loop.

Execution of loops is based on iterations. To run a block of code in a loop, one needs to set a condition and set its number of iterations. Each time the condition is true, and the block of code executes once, it is counted to be one iteration. Before moving to the next iteration, one needs to increase the count of iterations to two. This is called incrementing a loop.

For example, if you need to print numbers 0 to 4, you will execute a block of code with the Print statement in five iterations. With each passing iteration, you will increment the count by one.

Benefits of Loops

Below are the two important benefits of loops:

  1. Reduces lines of code
  2. Code becomes easier to understand

Different Types of Loops

Loops make our code more manageable and organized. Let us now see what the different types of loops are:

  1. While Loop
  2. For Loop
  3. Nested Loop

While Loop

The While loop can execute a set of commands till the condition is true. While Loops are also called conditional loops. Once the condition is met then the loop is finished.

Implementing While Loop in Pictoblox

The Repeat Until () block is a Control Block and a C block. Blocks held inside this block will loop until the specified Boolean statement is true, in another case, the code beneath the block (if any) will execute. This loop is in similar nature to a while loop.

Activity 1: Counting Numbers

Let’s create a code to print 1-9 numbers on the LED of Quarky.

 

Coding Steps

Follow the steps below:

  1. Open PictoBlox App, go to My Space, and click on + button to create a new file.
  2. Connect Quarky to PictoBlox.
  3. Drag when green flag clicked block from Events.
  4. Next, make a variable and name it Count. Use a set my variable to ()block from the Variables palette and set the Count variable to 1.
  5. Add a repeat until ()block from the Control palette. Use > (greater than) block from the Operators palette. To check if the variable Count is greater than 9 or not.
  6. If the variable Count is less than 10 then we want the Quarky display LED to be in green color.
  7. Add a wait ()seconds block from the Control palette with value 1. So, the code will wait for one second and then change the variable Count value by 1 using the change my variable by () block from the Variables.
  8. Finally, click the green flag to run the script.
  9. Save the file as While Loop.

Activity 1: Output

For Loop

For loop is needed for iterating over a sequence. A for loop executes a specific number of times.

For Loop in Pictoblox

We use repeat () block for executing the for loop in PictoBlox.

Blocks inside the repeat block will loop a given number of times, before allowing the script to continue. If a decimal is an input, the number is rounded up.

Activity 2: Tobi Reciting Table

 

Follow the steps below:

  1. Open PictoBlox App, go to My Space, and click on + button to create a new file.
  2. Add an ask () and wait for block to get the number whose table the user wants Tobi to recite.
  3. Make a variable named times and set it to 0 using set () to () block from Variables.
  4. Add a repeat block and write 10 in its space as we want Tobi to say from 1 to 10.
  5. Add a change () by () block to increment the value each time the repeat block is called.
  6. Add a say () block below the change () by () block.
  7. Next, add four join () () blocks from the Operators palette together to make a dynamically generated sentence.
  8. Finally using the answer from Sensing palette and times from the Variables palette as shown in the script below.
  9. Finally, add a when flag clicked block above the entire script.
  10. Click the green flag to run the script.
  11. Save the file as For Loop.

Activity 2: Output

Make sure you finish and submit the assignment, as well as take the quizbefore moving on to the next lesson.

Table of Contents