Course 2 / Lecture 3:

# Using loops to repeat tasks

You will learn how to use loops to repeat tasks. You will learn the term infinite loop and a loop with a specified number of steps. You will see how loops can be used to simplify programs. This lecture includes tasks to move your robot around a route using loops. You will see how various patterns can be followed with the help of loops.

Figure 1 - Using loop

## What is a loop in programming?

Loop - it is a control structure. It can encapsulate one or more instructions (these are called loop core). The loop repeats the loop core by given times or while a condition is met.

## Discover the 'forever' loop in SNAP!

In programming, loops are used very often, so it is important to understand how to use them. In this task, you will discover the first loop type block in SNAP called 'forever'. It can be found in the Control category. This block executes the blocks inside continuously until you stop the program by pressing the red stop button on the top right corner or use a special block to break it. You can easily try out how it works. Just drag a 'forever' block and drop it into the 'Scripts' field then put the blocks you want to repeat into the 'forever' one. Look at the program on Figure 2 below for an example and try it out! What did you recognize? The cursor continuously turns 90 degrees to the right and move 100 steps forward. This is what 'forever' block does. It executes the blocks in it continuously.

Figure 2 - Example for using the 'forever' block in SNAP

## Discover the 'repeat' block in SNAP!

In this task, you will learn how to use the 'repeat [times]' loop type of block. This block executes the blocks inside it continuously like the other loop type of blocks do, but in case of this block you should specify how many times the loop needs to iterate. So you can determine how many times the blocks in the 'repeat' block need to be executed. Look at the example on Figure 3 and try it! You certainly noticed that the cursor has turned to the right and has moved forward only three times. It is because there is a value 3 in the times field of the repeat block. Type another number and see what happens!

Figure 3 - Example for using the 'repeat' block in SNAP

## What is the difference between 'forever' and 'repeat' blocks?

The 'forever' block executes the blocks in it continuously until you stop the program or use a special block (for example the 'stop' block in Control category) to break it, while in case of 'repat' block you need to specify a number how many times you want to execute the blocks inside it.

## Discover the 'repeat until' block in SNAP!

During this task, you will learn how to use the 'repeat until [condition]' loop type of block. You can find it in the Control category. It has a condition field where you should provide a predicate type of block. The predicate type of blocks always return 'true' or 'false' value depending on something. For example, the 'key [key] pressed?' block found in the Sensing category returns true, if the selected key pressed on your keyboard. Click on the block to try it yourself! The 'repeat until' block executes the blocks inside it continuously until the predicate type of block in its condition field returns true. So while the loop is running, the program check the condition and if it is true, the loop will break. If not, it will execute the blocks inside it and as a next step it will check the condition again. It is easier to understand if you see a simple example for it. Snap the blocks as can be seen on Figure 4 and execute it! The cursor will go around quickly until you stop it by pressing the space key.

Figure 4 - Example for using the 'repeat until' block in SNAP

## What is the difference between 'repeat' and 'repeat until' blocks?

The 'repeat [times]' block executes the blocks in it exactly as many times as you provided in its times field. In case of 'repeat until [condition]' block, you cannot specify how many times the loop should iterate, but you can provide a condition which if becomes true, the loop will break.

## Discover the 'for' block in SNAP!

Let's get to know the 'for i = [start] to [end]' block. Find the block in Control category then drag it to the Scripts field. In the block, you can see a variable called 'i'. This variable takes the value of the start field at first iteration then it is increased by one in every iteration until it reaches the end field's value. You can use this variable to keep track which iteration the loop currently performs. Look at the simple example on Figure 5 and execute it! This example almost does the same as the previous ones but prints the currently iteration number holded by the variable 'i'. Enter number 2 to the start field and number 4 to the end field then see what the loop will do! The 'join' block only concatenates the strings and variables' values entered it. To copy variable 'i', just drag it from the 'for' block.

Figure 5 - Example for using the 'for' loop in SNAP

## What are the two most important similarities and differences between the 'repeat' and 'for' blocks?

Similarity: Each loop type block executes a specified number of iterations.
Difference: Using the 'repeat [times]' block, you need to enter a number how many times the loop needs to iterate. While using the 'for i = [start] to [end]' block, you need to provide a start value for variable 'i' to start iterating and an end value to stop iterating when it is reached by variable 'i'. In this case, you can also access the iteration number, but you can not do this while using 'repeat' loop.

## Write a program that changes three emojis on the Brick's screen in a loop!

You should write a program that changes the emojis continuously on the Brick's LCD screen in a loop. Please use a 'forever' block for this program and display a happy, neutral and a sad face on the device. Please place the blocks under each other like on Figure 6. The 'forever' block ensures you to see different emojis on the Brick's LCD until you stop the program with the red button. In the loop, you should use 'wait [1] secs' blocks. Without 'wait' blocks, the emojis change really fast so you can not see them clearly. The 'wait' block allows you to see the faces after each other.

Figure 6 - A program for displaying happy, neutral and sad faces

## Write a program that drives the robot forward then turns it right!

Let's write a program to drive the robot forward and turn it right! If you passed Course 1, you can easily write this program. The Sniffer robot works with 2 motors. So if you would like to drive the robot forward, you should drive both of the motors at the same time. To resolve this task, you need to search for the 'Drive [motors] [direction] [time] sec' block. Using it, you can set the motors you would like to drive. You can also set the direction and the time of the drive. Please place the blocks under each other like in Figure 7 and start the program with the green flag icon to see how it works.

Figure 7 - A simple program to drive the robot forward then turn it right

## Upgrade the program that drives the robot forward then turns it right for 4 times!

Now, you should upgrade the program to drive the robot forward and turn it right for 4 times. In the previous task, you wrote the program that drives the robot to the requested directions once. Let's suppose that the robot should be do the same 100 times. It would be a really long time to place the required 'Drive' blocks to the Scripts field and attach them to each other. But there are loop blocks and you learnt how to use them. You just need to use the right one and it will be a few second to upgrade the previous program. Do you know which one you should use? Yes, you need to use the 'repeat [times]' one. Let's upgrade it then try it! If you need some help, please look at Figure 8.

Figure 8 - Using the repeat block to drive the robot for 4 times

## Upgrade the program to display the iteration number on the LCD display of the Brick!

Do you remember which was the block you could use to display the iteration number? Yes, it was the 'for i = [start] to [end]' loop type of block. So you need to use this block instead of 'repeat' to be able to acces the iteration number. Before displaying any text on the Brick's LCD, it is recommended to delete its currently content by using the 'Clean LCD' block found in Lego category. To set the text to be displayed, you should use the 'LCD text [text] position x: [x_position] y: [y_position]' block. The text field is responsible for the text to be displayed on the LCD and the x_position and y_position for the text's position on the display. Finally, you need to use the 'Update LCD' to update the screen to display the set text. Snap the blocks as can be seen on Figure 10 then please execute the program. There is a 'wait' block for the display to have time to refresh its content.

Figure 10 - Driving the robot 4 times while displaying the iteration number