forloopsinalice1 stephen cooper wanda dann randy pausch barb ericson oct 2009 counted (for) loops in...

15
ForLoopsInAlice 1 Stephen Cooper Wanda Dann Randy Pausch Barb Ericson Oct 2009 Counted (For) Loops in Alice

Upload: jonah-wright

Post on 19-Jan-2016

218 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: ForLoopsInAlice1 Stephen Cooper Wanda Dann Randy Pausch Barb Ericson Oct 2009 Counted (For) Loops in Alice

ForLoopsInAlice 1

Stephen Cooper

Wanda Dann

Randy Pausch

Barb Ericson

Oct 2009

Counted (For) Loops in Alice

Page 2: ForLoopsInAlice1 Stephen Cooper Wanda Dann Randy Pausch Barb Ericson Oct 2009 Counted (For) Loops in Alice

ForLoopsInAlice 2

Learning Goals

• Introduce the concept of a counted loop– A loop that keeps a counter or index– Show how a loop leads to easier to read and

shorter code– Show an infinite for loop– Show a nested loop– Show a function call inside a loop

Page 3: ForLoopsInAlice1 Stephen Cooper Wanda Dann Randy Pausch Barb Ericson Oct 2009 Counted (For) Loops in Alice

ForLoopsInAlice 3

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 then disappear 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

Page 4: ForLoopsInAlice1 Stephen Cooper Wanda Dann Randy Pausch Barb Ericson Oct 2009 Counted (For) Loops in Alice

ForLoopsInAlice 4

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.

Page 5: ForLoopsInAlice1 Stephen Cooper Wanda Dann Randy Pausch Barb Ericson Oct 2009 Counted (For) Loops in Alice

ForLoopsInAlice 5

bunny.hop

Page 6: ForLoopsInAlice1 Stephen Cooper Wanda Dann Randy Pausch Barb Ericson Oct 2009 Counted (For) Loops in Alice

ForLoopsInAlice 6

One solution

• Creating the same instruction again and again is somewhat tedious and the code gets longer and longer.

Page 7: ForLoopsInAlice1 Stephen Cooper Wanda Dann Randy Pausch Barb Ericson Oct 2009 Counted (For) Loops in Alice

ForLoopsInAlice 7

Counted Loop

• A counted loop is an alternate to repeated code– Repeats instructions a counted number of times

Page 8: ForLoopsInAlice1 Stephen Cooper Wanda Dann Randy Pausch Barb Ericson Oct 2009 Counted (For) Loops in Alice

ForLoopsInAlice 8

Demo 07BunnyHop• Concepts illustrated in this example

– The loop instruction executes a definite number of times, specified by a count or index

– Using a loop instruction • saves time• is convenient • Easy to modify

Page 9: ForLoopsInAlice1 Stephen Cooper Wanda Dann Randy Pausch Barb Ericson Oct 2009 Counted (For) Loops in Alice

ForLoopsInAlice 9

Demo 07Carousel

• Concept illustrated in this example– If “Infinity times” is selected for a loop, this means the loop

will run until the program is shut down– This is an infinite loop

Page 10: ForLoopsInAlice1 Stephen Cooper Wanda Dann Randy Pausch Barb Ericson Oct 2009 Counted (For) Loops in Alice

ForLoopsInAlice 10

More complicated loops

• It is also possible to place a loop statement within another loop statement

• This is called a nested loop

Page 11: ForLoopsInAlice1 Stephen Cooper Wanda Dann Randy Pausch Barb Ericson Oct 2009 Counted (For) Loops in Alice

ForLoopsInAlice 11

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.

Page 12: ForLoopsInAlice1 Stephen Cooper Wanda Dann Randy Pausch Barb Ericson Oct 2009 Counted (For) Loops in Alice

ForLoopsInAlice 12

Demo 07FerrisWheel

• Concept illustrated in this example– The inner loop runs completely each time the

outer loop runs once.– An outer loop that executes 2 times and an

inner loop that executes 5 times will actually execute the inner loop 10 times.

Page 13: ForLoopsInAlice1 Stephen Cooper Wanda Dann Randy Pausch Barb Ericson Oct 2009 Counted (For) Loops in Alice

ForLoopsInAlice 13

Using a function

• 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.

• Demo: 07LoopWithFunctionCall

Page 14: ForLoopsInAlice1 Stephen Cooper Wanda Dann Randy Pausch Barb Ericson Oct 2009 Counted (For) Loops in Alice

ForLoopsInAlice 14

Challenges

• Copy 07BunnyHop to 07BunnyOut– So that as the bunny nears the broccoli Papa rabbit (use Hare

from the Animals folder) appears at the garden gateway and taps his foot

– Have the bunny turn and hop out of the garden • Using a loop

• Copy 07BunnyHop to 07BunnySquare– And have the bunny hop in a square using a

nested loop• Loop 4 times

– Loop 3 times» Bunny hop

– Bunny turn ¼ revolution

Page 15: ForLoopsInAlice1 Stephen Cooper Wanda Dann Randy Pausch Barb Ericson Oct 2009 Counted (For) Loops in Alice

ForLoopsInAlice 15

Summary

• A for loop is similar to a while loop – But is useful when you need a counter or

index in the loop

• An infinite loop never stops

• Loops can be nested inside of other loops

• You can call a function inside the test for a loop