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:
- Reduces lines of code
- 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:
- 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.
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:
- Open
PictoBlox App, go toMy Space, and click on+button to create a new file. - Connect
QuarkytoPictoBlox. - Drag when green flag clicked block from
Events. - Next, make a variable and name it Count. Use a set my variable to ()block from the
Variablespalette and set the Count variable to 1. - Add a repeat until ()block from the
Controlpalette. Use > (greater than) block from theOperatorspalette. To check if the variable Count is greater than 9 or not. - If the variable Count is less than 10 then we want the Quarky display LED to be in green color.
- 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. - Finally, click the green flag to run the script.
- 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:
- Open
PictoBlox App, go toMy Space, and click on+button to create a new file. - Add an ask () and wait for block to get the number whose table the user wants Tobi to recite.
- Make a variable named times and set it to 0 using set () to () block from
Variables. - 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 () block below the change () by () block.
- Next, add four join () () blocks from the
Operatorspalette together to make a dynamically generated sentence. - Finally using the answer from
Sensingpalette and times from theVariablespalette as shown in the script below. - Finally, add a when flag clicked block above the entire script.
- Click the green flag to run the script.
- Save the file as
For Loop.
Activity 2: Output
Make sure you finish and submit the assignment, as well as take the quiz, before moving on to the next lesson.