Summer Computing Workshop PROGRAMMING WITH SCRATCH Session 4 Loops At the heart of their functionality, loops enable blocks of code to be executed more than once Loops represent a simple concept that can be used to create extremely dynamic and flexible programs Loops are very important for two reasons: efficiency and flexibility In the project of last session, you were instructed to basically run the program twice. In order to accomplish this you had to copy the existing blocks and append them to the end of the script effectively doubling the number of blocks present. It worked, but just imagine what would happen if you had to go through the script ten, one-hundred, or even a thousand times! Assuming you were able to create a program of that magnitude, what if a change had to be made? You would be required to make that change for each copy you made! Loops allow the program to contain only one copy of the code and go through it as many times as you need to. This is much more efficient Repeat In scratch, the simplest loop is the repeat block. It closely resembles the for loop found in other programming languages The repeat block expects the programmer to specify a number which will indicate exactly how many times it will repeat In this example, the move block will be repeated ten times. To find out how far the sprite will move in total, all we have to do is some quick multiplication to realize it will move 100 steps This block is used when the number of times to repeat is known before the block is encountered, but sometimes, there is no way of knowing exactly how many times a loop must execute. In cases of uncertainty, a slightly different loop must be used Repeat until The repeat until loop, as the name implies, repeats until a given Boolean expression is true It is very similar to the while loop found in other programming languages; the only difference is the Boolean expression is switched. Instead of repeating until a value is true, a while loop repeats until a value is false Repeat until loops are perfect when combined with user input This allows the user to interact with the program indefinitely; loops such as these are found in almost every program in some way, shape, or form Although the two loops mentioned thus far appear to behave differently, they are the same loop. With the use of variables, the repeat until loop can be made to work exactly like a repeat loop Forever In some cases, a loop must continue repeating the entire time the program is running. When this is required a forever loop is often used Notice that there is no “connector” on the bottom of this block. This is because any blocks located below it in the script would be impossible to reach Like the repeat until loop, the forever loop is commonly used in conjunction with user input. Forever In session 2, we used the “when key is pressed” blocks to move a sprite around on the screen with the arrow keys; however, we were unable to move diagonally by pressing two keys at once With the use of loops, multi-key input is now possible Limited functionality from Session 2 New and improved design Forever if Scratch has one more loop that deserves a mention; the forever if block It lacks a connector on the bottom like the forever loop but is able to accept a conditional block After a bit of testing (I encourage you to test this as well) we realized it was just a combination of two other flow control blocks; the forever and the if blocks These two scripts behave in exactly the same way = Nesting Like the conditional branch structures in the previous session, loops can, and sometimes must, be nested. The behavior of these loops is often trivial, but sometimes they can be very complex This script moves the sprite 100 steps (2 * 10 * 5) What does this one do? On Your Own Write a script that draws a box of a user-defined size on the screen using the pen tool. Create a 5x5 grid of these boxes Session 4 Questions 1. The expression in the predicate part of an if/else structure evaluates to what kind of value? 2. What part of the structure would execute if the predicate value was true? 3. In your own words, what does a loop do? 4. One type of single-character input can be used to start a script, while the other can be used ______ the script. 5. If a certain block of number of blocks needed to be executed several times, what control structure would you use? 6. If a loop executes 5 times and there was a block inside that moved the sprite 3 steps, how many steps would it move in total?