Presentation is loading. Please wait.

Presentation is loading. Please wait.

Repetition: Definite Loops Alice. Repetition In many kinds of animations, especially simulations and games, some actions happen again and again. Example:

Similar presentations


Presentation on theme: "Repetition: Definite Loops Alice. Repetition In many kinds of animations, especially simulations and games, some actions happen again and again. Example:"— Presentation transcript:

1 Repetition: Definite Loops Alice

2 Repetition In many kinds of animations, especially simulations and games, some actions happen again and again. Example: Gallery games where targets appear randomly on screen and are caught or shot down, only to appear elsewhere in the scene Of course, actions are made to happen again and again by running an animation instruction (or a method) more than once

3 Example A bunny sneaks into a garden and wants to eat the broccoli. The bunny will need to hop several times to get to the broccoli.

4 bunny.hop

5 One solution What is the problem with this solution?

6 Counted Loop A counted loop is an alternative way to write repetitive code Repeats instructions a counted number of times In Alice,

7 Code The loop instruction executes a definite number of times, specified by a count Using a loop instruction saves time is convenient

8 Demo

9 Infinity times… If “Infinity times” is selected for a loop, this means the loop will run until the program is shut down

10 Example To make the carousel go round continuously in an amusement park world: Built-in method

11 More complicated loops It is also possible to place a loop statement within another loop statement This is called “nested loops”

12 An example of nested loops The whole Ferris wheel will rotate clockwise, while the two inner wheels will rotate counterclockwise. The inner wheels should perform 2 revolutions for each outer loop revolution.

13 Rotating each of the wheels Rotating the outer wheel 10 times Rotating the inner wheels 2 times

14 The entire code The two loops structures, inner wheel loop nested within the outer wheel loop. How many times does the inner loop execute?

15 Using a question A loop count can be computed by calling a question that returns a number value. The loop instruction automatically rounds the returned value to the nearest whole number.

16 Assignment Read Chapter 7- 1 Lab 7-1


Download ppt "Repetition: Definite Loops Alice. Repetition In many kinds of animations, especially simulations and games, some actions happen again and again. Example:"

Similar presentations


Ads by Google