23
CS320n –Visual Programming Indefinite Loops (Slides 7-2) hanks to Wanda Dann, Steve Cooper, and Susan Rodger or slide ideas.

CS320n –Visual Programming Indefinite Loops (Slides 7-2) Thanks to Wanda Dann, Steve Cooper, and Susan Rodger for slide ideas

Embed Size (px)

Citation preview

Page 1: CS320n –Visual Programming Indefinite Loops (Slides 7-2) Thanks to Wanda Dann, Steve Cooper, and Susan Rodger for slide ideas

CS320n –Visual Programming

Indefinite Loops

(Slides 7-2)

Thanks to Wanda Dann, Steve Cooper, and Susan Rodger for slide ideas.

Page 2: CS320n –Visual Programming Indefinite Loops (Slides 7-2) Thanks to Wanda Dann, Steve Cooper, and Susan Rodger for slide ideas

Visual Programming Indefinite Loops 2

What We Will Do Today• Learn about indefinite loops (while)

• Learn about event loops

• Look at a case study involving loops

Page 3: CS320n –Visual Programming Indefinite Loops (Slides 7-2) Thanks to Wanda Dann, Steve Cooper, and Susan Rodger for slide ideas

Visual Programming Indefinite Loops 3

Repetition• Sometimes don’t know or can’t computer exactly

how many times a set of instructions are repeated

• Stopping is based on some condition (a boolean)

• Examples:– Game of Chess, how many moves until win

• Stop: when markers are in a check mate position

– The bunny world. Bunny two jump up and down until Bunny one done eating.

• Actually could roughly calculate this based on distances

Page 4: CS320n –Visual Programming Indefinite Loops (Slides 7-2) Thanks to Wanda Dann, Steve Cooper, and Susan Rodger for slide ideas

Visual Programming Indefinite Loops 4

Indefinite Repetition• In programs where a count of repetitions is not

known (indefinite), we can use one of two repetition control mechanisms:– While statement (today)– Recursion (next time)

Page 5: CS320n –Visual Programming Indefinite Loops (Slides 7-2) Thanks to Wanda Dann, Steve Cooper, and Susan Rodger for slide ideas

Visual Programming Indefinite Loops 5

While statement

• While some condition is true– execute statements 1 time, go and recheck condition

• To write a While statement, you need to know the condition that determines whether the loop will be repeated.

Page 6: CS320n –Visual Programming Indefinite Loops (Slides 7-2) Thanks to Wanda Dann, Steve Cooper, and Susan Rodger for slide ideas

Visual Programming Indefinite Loops 6

Example• In the garden scene from last time

• bunny one went around and ate up the food

• bunny two hopped in place forever

• what if we want bunny two to hop until bunny one is done eating all the food?– in other words we want bunny two to hop

while what is true?

Page 7: CS320n –Visual Programming Indefinite Loops (Slides 7-2) Thanks to Wanda Dann, Steve Cooper, and Susan Rodger for slide ideas

Visual Programming Indefinite Loops 7

Original Version

Page 8: CS320n –Visual Programming Indefinite Loops (Slides 7-2) Thanks to Wanda Dann, Steve Cooper, and Susan Rodger for slide ideas

Visual Programming Indefinite Loops 8

Change to Garden World• What condition can we check to determine

if bunny two should hop again or stop?

Page 9: CS320n –Visual Programming Indefinite Loops (Slides 7-2) Thanks to Wanda Dann, Steve Cooper, and Susan Rodger for slide ideas

Visual Programming Indefinite Loops 9

Example Two• A chase scene• hungry shark chasing

fleeing goldfish– repeat: fish swim away

from shark and shark swim toward fish

– shark swim distance a little more than fish swim distance

– eventually shark will catch up with fish

Page 10: CS320n –Visual Programming Indefinite Loops (Slides 7-2) Thanks to Wanda Dann, Steve Cooper, and Susan Rodger for slide ideas

Visual Programming Indefinite Loops 10

Storyboard• World.chase

while goldfish more than 0.5 meters from shark

Do in order

shark point at goldfish

Do together

shark swim (toward goldfish)

goldfish flee (away from shark)

resolve chase (shark eat goldfish or goldfish eat shark)

Page 11: CS320n –Visual Programming Indefinite Loops (Slides 7-2) Thanks to Wanda Dann, Steve Cooper, and Susan Rodger for slide ideas

Visual Programming Indefinite Loops 11

World.chase

methods to be written: top down design

Page 12: CS320n –Visual Programming Indefinite Loops (Slides 7-2) Thanks to Wanda Dann, Steve Cooper, and Susan Rodger for slide ideas

Visual Programming Indefinite Loops 12

Will the loop end?• What does it depend on?

Page 13: CS320n –Visual Programming Indefinite Loops (Slides 7-2) Thanks to Wanda Dann, Steve Cooper, and Susan Rodger for slide ideas

Visual Programming Indefinite Loops 13

Ending the Loop• Assume the goldfish and the shark

move a random distance– make the shark move forward between

0.1 and 0.6 meters– make the goldfish move between 0.0 and

0.3 meters– Shark will eventually catch up, and the

loop will end.– not sure how many steps it will take, but

the shark will eventually catch the goldfish

Page 14: CS320n –Visual Programming Indefinite Loops (Slides 7-2) Thanks to Wanda Dann, Steve Cooper, and Susan Rodger for slide ideas

Visual Programming Indefinite Loops 14

goldfish.flee

Page 15: CS320n –Visual Programming Indefinite Loops (Slides 7-2) Thanks to Wanda Dann, Steve Cooper, and Susan Rodger for slide ideas

Visual Programming Indefinite Loops 15

goldfish.random Motion

Will the goldfish ever move backwards?How could the random motion be made more realistic?

Page 16: CS320n –Visual Programming Indefinite Loops (Slides 7-2) Thanks to Wanda Dann, Steve Cooper, and Susan Rodger for slide ideas

Visual Programming Indefinite Loops 16

Shark.swim• Shark pointed in correct direction• move random distance

Page 17: CS320n –Visual Programming Indefinite Loops (Slides 7-2) Thanks to Wanda Dann, Steve Cooper, and Susan Rodger for slide ideas

Visual Programming Indefinite Loops 17

The loop will end– Geometrically, the fish can never move more

than 0.52 meters away• the distance formula

– The shark has a distance advantage and will eventually catch up. The loop will end.

0.30.3

0.3

0.52

Page 18: CS320n –Visual Programming Indefinite Loops (Slides 7-2) Thanks to Wanda Dann, Steve Cooper, and Susan Rodger for slide ideas

Visual Programming Indefinite Loops 18

General “Rule of Thumb” • As a general rule, a While loop should be

written so the loop will eventually end. – Requires that statements within the loop

change the conditions of the world such that the condition for the While statement will eventually become false.

• If the While loop never ends, it is an infinite while loop.

Page 19: CS320n –Visual Programming Indefinite Loops (Slides 7-2) Thanks to Wanda Dann, Steve Cooper, and Susan Rodger for slide ideas

Visual Programming Indefinite Loops 19

Begin – During – End Events• From chapter 7 tips and techniques

Page 20: CS320n –Visual Programming Indefinite Loops (Slides 7-2) Thanks to Wanda Dann, Steve Cooper, and Susan Rodger for slide ideas

Visual Programming Indefinite Loops 20

Storyboard• Helicopter circling the rabbit on the island

• rabbit turns to look at the helicopter as long as the helicopter is in front of him– no “Exorcist” rabbit

• 2 repeated actions– helicopter circling– rabbit moving head

• when rabbit can no longer see helicopter turn to look at camera

Page 21: CS320n –Visual Programming Indefinite Loops (Slides 7-2) Thanks to Wanda Dann, Steve Cooper, and Susan Rodger for slide ideas

Visual Programming Indefinite Loops 21

While something is true

Page 22: CS320n –Visual Programming Indefinite Loops (Slides 7-2) Thanks to Wanda Dann, Steve Cooper, and Susan Rodger for slide ideas

Visual Programming Indefinite Loops 22

Begin – During - End• Selecting the event

“While Something is True” creates 3 options

• begin: something done when event becomes true

• during: something done while event is true

• end: what to do when event is no longer true

Page 23: CS320n –Visual Programming Indefinite Loops (Slides 7-2) Thanks to Wanda Dann, Steve Cooper, and Susan Rodger for slide ideas

Visual Programming Indefinite Loops 23

Completed World