Lesson 11 : Loops

Lesson Description

In programming where we need to repeat certain lines of code at a specific interval or till a specified condition is met we use loops. In this lesson, students will be able to grasp the concept of loops in coding with the help of activities of printing 1-9 counting and 9-1 counting on Quarky LED display using loops.

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 a specified condition remains true. This condition is checked based on the loop’s control variable. Whenever this condition results in false, the loop terminates. It is very important to keep this thing in mind while programming that the condition should result 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.

Let us understand loops with a flowchart:

  1. Here every time the condition (Count < 5) is true, “Print count” gets executed. So, we do not have to write the “Print” statement multiple times. The loop takes care of that.
  2. What is important to note is every loop must have an exit condition. In our example, the exit condition is (Count < 5). The loop will exit when the condition becomes false.
  3. Also, most loops will have a variable which in programming terms is called a counter variable. The counter variable keeps track of how many times the loop is executed. In this example, the “count” variable is our counter.

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

 

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.

General Flow of While Loop

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 which case the code beneath the block (if any) will execute. This loop is in similar nature to a while loop.

Activity 1

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

Flow Chart

Code

  1. Drag When green flag clicked block from Events palette. 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.
  2. Add a repeat until () block from the Control palette. Use > (more than) block from the Operators palette. To check if the variable Count is less than 10 or not.
  3. If the variable Count is less than 10 then we want the Quarky display LED to be in green color.
  4. 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 palette.

Test

Activity 2

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

Test

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 amount of times, before allowing the script to continue. If a decimal is input, the number is rounded up.

repeat ()

Activity: Tobi Reciting Table

  1.  Add an ask () and wait for block to get the number whose table the user wants Tobi to recite.Counting a Table
  2. Make a variable and set it to 0.
  3. Add a repeat block and write 10 in its space as we want Tobi to say from 1 to 10.Counting a Table
  4. Add a change () by () block to increment the value each time the repeat block is called.
  5. Next, make Tobi recite the table by using the blocks shown in the script below.
  6. Finally, add a when flag clicked block above the entire script.Counting a TableTable counting

Assignment

Before you move on to the next lesson, a small assignment awaits you!

You must upload the PictoBlox program you created in this activity to the website. Submitting the assignment is a must in order to receive the certificate after completing the course.

Follow the steps below to upload your assignment:

  1. Click on Browse.
  2. Search and Select your saved Project file(.sb3) and Click Open.
  3. Click on Upload to submit the assignment.
evive Alert
The file type allowed is the SB3 file generated from the PictoBlox program. The maximum file size allowed is 5 MB.

Good luck!

Table of Contents