In programming, 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:
- 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.
- 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.
- Also, most loops will have a variable that is called a counter variable in programming terms. 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:
- Reduces lines of code
- code becomes easier to understand
Types of Loops
Loops help make our code more manageable and organised. Let’s now explore the various types of loops available:
- While Loop
- For Loop
- 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.
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.
The While loop executes a set of commands as long as the condition remains true. While Loops are also known as conditional loops. When the condition is no longer met, the loop concludes.
For Loop
The For loop is used for iterating over a list of items individually. It facilitates the repetition of a certain action a set number of times, simplifying the process of working with a group of items sequentially.
|
For Loop in PictoBlox
We use a repeat () block for executing the for loop in PictoBlox.
Blocks inside the repeat block will loop several times before allowing the script to continue. If a decimal is input, the number is rounded up.
Activity 1: Make a Square
Pen Extension Blocks
We will be using the following blocks from the pen extension in the next topic.
- When you run the pen down block, you enable a mode where, whenever the sprite moves, it will draw lines on the stage.
- When you run the pen up block, you enable a mode where the sprite does not draw while moving.
- To erase all the lines, we will use erase all block.
To make a square, follow the steps below:
- Continue from the script of the last topic.
- Click the pen down block.
- Then, click the move 100 steps block.
- Next, click the turn clockwise 90 degrees block.
- Repeat steps 2 & 3 three more times.
Editing the costume
- Click the Costumes tab, and you’ll see the pencil in the costume area on the left.
- Select the full costume by clicking on one end of the editor and dragging the mouse over and across the costume. The pencil will get highlighted. Now, we can move the pencil around.
- Click on the pencil and drag it in such a way that the tip of the pencil overlaps the center dot on the editor. After aligning them release the pencil.
Once you do it, you’ll observe that in the stage, the line is now at the tip of the pencil. Success!
Making the Square using Script
- Now, let’s create the script to draw the square by stacking all the blocks together.
- To draw the square, you had to click each of the 3 blocks 4 times. To make a script that does the same, you need to add all 3 blocks in the given sequence 4 times!
- Now when you click on the modified script, you’ll be able to see the pencil moving and drawing side of the square on by one.
- You can simplify all this by using a repeat block. Just add it around the 3 blocks and write 4 in the space.
- Place a pen down block above the repeat block to draw the square and place a when () key pressed block above the entire script. That’s it.
And your square is ready! Save the File as Square.
Activity 2:Reciting Table
In this activity, you will make a bot that takes the number input from the user and recites the table of that number.
Ask and Wait for Block
The Sensing palette contains blocks that help sprites detect and respond to different conditions and events. It has two blocks that help us take the inputs.
The block prompts the user to enter text and stores the response in a variable for use in the program. | |
The block retrieves the text entered by the user in response to the ask () and wait block. |
Follow the steps below:
- Create a New file in PictoBlox.
- Create two variables – N and Times.
- Add when green flag clicked from Events
- Add an ask () and wait block to get the number whose table the user wants Tobi to recite.
- Add set () to () block from the Variables Set N to answer by dropping the answer block inside the block.
- Next, add set () to () block and set Times to 0.
- Add a repeat () block and write 10 in its space as we want Tobi to say from 1 to 10.
- Add a change () by ()block to increment the value each time the repeat () block is called.
- Add a say () for () block below the change () by ()
- Next, add four join () () blocks from the Operators palette together to make a dynamically generated sentence.
- Finally, use the N and Times from the Variables palette to generate the following: N x Times = (N*Times).
- Click on the Green Flag to run the code.
- Save the File as a Reciting Table.