CompSci 4Chap 7 Sec 2Nov 1, 2005Prof. Susan RodgerNote: thanks to Wanda Dann and Steve Cooper for slide ideasAnnouncements• Read Chapter 9.1 for next time• Assignment 6 due Nov 8– Finish classwork from Thursday first…What we will do today• Lecture on Chap 7 Sec 2 and Tips and Tech.– While loop – indefinite loop– Event Loops• ClassworkLast time -Loop – definite number• What is the output?Repetition• Sometimes don’t know exactly how many times a set of instructions are repeated.• Stopping is based on a condition•Example: – Game of Chess, how many moves until win– Stop: when markers are in check mate positionIndefinite Repetition• In programs where number of repetitions not known in advance, can use– While statement (today)– Recursion (Chap 8) - laterWhile statement• While some condition is true– execute instructionsconditiontrue?execute instructionsnoyesexitloopExample• Common feature in popular “action films”is a chase scene• Example: 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 and eat fishStoryboardWorld.chaseWhile goldfish more than .5 meters from sharkDo in ordershark point at goldfishDo togethershark swim (toward goldfish)goldfish flee (away from shark)shark eat goldfishshark.Swim, shark.eat and goldfish.flee in bookWorld.chaseShark will catch goldfish• How do you know the shark will eventually catch the goldfish?– Shark always moves 0.4 meters towards goldfish– Goldfish moves randomly away from shark at most .2 meters– Shark will eventually catch up, the loop will endGeneral “Rule of Thumb”• As a general rule, a While loop should be written so the loop will eventually end– Requires statements inside the loop change the conditions of the world such that condition for While eventually becomes false• If While loop never ends– Infinite while loopPractice – From Bunny eats Broccoli• Replace with while loop, bunny hops until close to closestBroc (< 1 meter)Practice – Eat Broccoli until all invisibleAssume method bunny.hopToClosestBroccoliAndEatIt existsReWrite with while loop• Eat broccoli (broc1, broc2, and broc3) until gone (all invisible)• Assume at least one of the brocs is visible when startClasswork – Can Cinderella and Handsome Prince ever get together?• Objects– Cinderella, HandsomePrince, ballCinderella, ballPrince•Setup – Cinderella and Prince 10 units each from middle• Game – both Cinderella and Handsome prince move forward randomly– If both reach the middle, they connect!– If either reach the middle alone, then that one starts overSetup• They start by touching hands.– ballCinderella (invisible) is at same location as Cinderella– ballPrince (invisible) is at same location as Prince• They both turn and walk 10 units• They turn and face.PlayRepeat forever• They both move randomly forward• Cinderella gets to the middle first alone.– She turns around– Goes forward 10 units– She turns back around• They are both in middle at same time– Message displayed and stopWorld. My first methodNote: variable, where is it set and used?What is repeated a lot?• Turn around• Move forward 10 units• Turn back aroundSetupturnAround• Turn halfway around• Move forward 10 units, 1 unit at a time• Turn back aroundrandomIntHow to move forward randomly?• Move forward randomly 1 to 3 units, but not past marker ball?• Use randomIntMore Detailed Play• Repeat over and over– Cinderella moves forward 1 to 3 units, but not past the ballCinderella, her spot in the middle– Prince moves forward 1 to 3 units, but not past the ballPrince, his spot in the middle– If both are in the middle, display a message, then stop.– If just one in the middle, then that one starts
View Full Document