11
Summer Computing Workshop

Summer Computing Workshop. Session 4 Loops At the heart of their functionality, loops enable blocks of code to be executed more than once Loops represent

Embed Size (px)

Citation preview

Page 1: Summer Computing Workshop. Session 4 Loops  At the heart of their functionality, loops enable blocks of code to be executed more than once  Loops represent

Summer Computing Workshop

Page 2: Summer Computing Workshop. Session 4 Loops  At the heart of their functionality, loops enable blocks of code to be executed more than once  Loops represent

Session 4

Page 3: Summer Computing Workshop. Session 4 Loops  At the heart of their functionality, loops enable blocks of code to be executed more than once  Loops represent

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

Page 4: Summer Computing Workshop. Session 4 Loops  At the heart of their functionality, loops enable blocks of code to be executed more than once  Loops represent

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

Page 5: Summer Computing Workshop. Session 4 Loops  At the heart of their functionality, loops enable blocks of code to be executed more than once  Loops represent

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

Page 6: Summer Computing Workshop. Session 4 Loops  At the heart of their functionality, loops enable blocks of code to be executed more than once  Loops represent

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.

Page 7: Summer Computing Workshop. Session 4 Loops  At the heart of their functionality, loops enable blocks of code to be executed more than once  Loops represent

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

New and improved design

Limited functionality from Session 2

Page 8: Summer Computing Workshop. Session 4 Loops  At the heart of their functionality, loops enable blocks of code to be executed more than once  Loops represent

Forever if

Page 9: Summer Computing Workshop. Session 4 Loops  At the heart of their functionality, loops enable blocks of code to be executed more than once  Loops represent

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?

Page 10: Summer Computing Workshop. Session 4 Loops  At the heart of their functionality, loops enable blocks of code to be executed more than once  Loops represent

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

Page 11: Summer Computing Workshop. Session 4 Loops  At the heart of their functionality, loops enable blocks of code to be executed more than once  Loops represent

Session 4 Questions1. 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?