CS 320 n Visual Programming Definite Counted Loops

  • Slides: 21
Download presentation
CS 320 n –Visual Programming Definite / Counted Loops (Slides 7 -1) Thanks to

CS 320 n –Visual Programming Definite / Counted Loops (Slides 7 -1) Thanks to Wanda Dann, Steve Cooper, and Susan Rodger for. Visual slide. Programming ideas. Definite / Counted Loops

What We Will Do Today • Learn how what definite loops are and how

What We Will Do Today • Learn how what definite loops are and how to use them in a program Visual Programming Definite / Counted Loops 2

Repetition • In many kinds of animations, especially simulations and games, some actions happen

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 • Actions are made to happen again and again by running an animation instruction (or a method) more than once Visual Programming Definite / Counted Loops 3

Computers are Fast • This is why we use computers. • The number of

Computers are Fast • This is why we use computers. • The number of instructions a CPU can carry out is mind boggling – Intel Processor: 3. 20 gigahertz. – CPU doing 3, 200, 000 instructions per second. – Simple instructions like adding two numbers, looking up the value of a variable • Lots of simple instructions can so quickly can accomplish a lot – 3 D animations, working with DNA, complex simulations. Visual Programming Definite / Counted Loops 4

Example • A bunny sneaks into a garden and wants to eat the broccoli.

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. Visual Programming Definite / Counted Loops 5

bunny. hop Visual Programming Definite / Counted Loops 6

bunny. hop Visual Programming Definite / Counted Loops 6

bunny. hop • Makes bunny hop up and down, making a sound and traveling

bunny. hop • Makes bunny hop up and down, making a sound and traveling forward 0. 8 meters • Code on next slide • How do we get the bunny to hop enough times to get over to the broccoli? Visual Programming Definite / Counted Loops 7

One solution • Does this work? • Are there any problems with it? Visual

One solution • Does this work? • Are there any problems with it? Visual Programming Definite / Counted Loops 8

Counted Loop • A counted loop is an alternative way to repetitive code •

Counted Loop • A counted loop is an alternative way to repetitive code • Repeats instructions a counted number of times • One of the commands available in Alice Visual Programming Definite / Counted Loops 9

Code for Loops • The loop instruction executes a definite number of times, specified

Code for Loops • The loop instruction executes a definite number of times, specified by a count. (Set when insert loop) • Using a loop instruction – saves time when coding – is convenient, easy to change count – can use a function that returns a number in place of the count Visual Programming Definite / Counted Loops 10

Modify the Bunny Animation • Make the bunny hop over to a broccoli and

Modify the Bunny Animation • Make the bunny hop over to a broccoli and eat it • Then hop to next broccoli and eat it • move broccoli so not all together • only do with 3 broccoli – fairly easy to expand to more broccoli Visual Programming Definite / Counted Loops 11

Infinity times… • If “Infinity times” is selected for a loop, this means the

Infinity times… • If “Infinity times” is selected for a loop, this means the loop will run until the program is shut down Visual Programming Definite / Counted Loops 12

Example 1: Add a bunny • Add a bunny to the broccoli scene •

Example 1: Add a bunny • Add a bunny to the broccoli scene • this bunny should jump up and down while the other bunny eats all the broccoli Visual Programming Definite / Counted Loops 13

Does this Work? Visual Programming Definite / Counted Loops 14

Does this Work? Visual Programming Definite / Counted Loops 14

How Do We fix This? • Get both bunnies to move – one definitely

How Do We fix This? • Get both bunnies to move – one definitely (the one that eats the broccoli) – one indefinitely (the one that hops up and down until the movie stops? ) Visual Programming Definite / Counted Loops 15

Example 2: Carousel • To make the carousel go round continuously in an amusement

Example 2: Carousel • To make the carousel go round continuously in an amusement park world: Visual Programming Definite / Counted Loops 16

More complicated loops • What can be placed in a loop? – any number

More complicated loops • What can be placed in a loop? – any number of other commands – including other loops – a loop within a loop – this is a called a nested loop • Example in book: a double ferris wheel Visual Programming Definite / Counted Loops 17

An example of nested loops The whole Ferris wheel will rotate clockwise, while the

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. Visual Programming Definite / Counted Loops 18

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

Rotating each of the wheels Rotating the outer wheel 10 times Rotating the inner wheels 2 times Visual Programming Definite / Counted Loops 19

The Complete Program The two loops structures, inner wheel loop nested within the outer

The Complete Program The two loops structures, inner wheel loop nested within the outer wheel loop. How many times does the inner loop execute? Visual Programming Definite / Counted Loops 20

Using a Function for Count • A loop count can be computed by calling

Using a Function for Count • A loop count can be computed by calling a function that returns a number value. • The loop instruction automatically rounds the returned value to the nearest whole number. Visual Programming Definite / Counted Loops 21