6 Using Loop Instruction 5.1Using Loop InstructionDrag Loop instruction from bottom of Method EditorIt prompts you for amount of times to loopCreates empty slot where instructions can be placed
7 Computing Number of Times to Repeat 5.1Computing Number of Times to RepeatWhat if you want something to loop and NOT have to specify a particular number of repetitions?Example: getting a ball to roll across into a goal regardless of where the ball is initially placedUse the soccerBall’s distance to function to calculate distance to goalLoop uses truncation: decimal portions of numbers are discardedExamples: objects loop 4 times not 4.8 or 3 times not 3.4
8 Loops Controlled by Variable Ask user for amount of times a lion should roarPlace value in variableLoop the amount of times user entered
9 Loop With Function Call Loop count can be computed by calling a function that returns a number valueLoop instruction automatically rounds returned value down to whole number
10 Infinite Loops The loop that never stops 5.1Infinite LoopsThe loop that never stopsUse for objects that shouldn’t stopIf placed in consecutive order…the next instructions will NEVER occur, since the loop NEVER ends!Place an infinite loop into a Do Together structure with other items.
11 Infinite Loop Background Example Infinite loops are good for moving objects in backgroundThis example simulates moving water by changing texture of waterCalled from main method
12 Infinite Loop Continual Motion Helicopter blades are turning while helicopter rescues diver
13 Checkpoint What type of structure is the Loop instruction? What are three different types of loops?What are different ways you can use a finite non fixed loop?What is an infinite loop?
14 The While Instruction Combines Loop and If/else statements 5.2The While InstructionCombines Loop and If/else statementsIs a loop that repeats as long as its Boolean condition is trueCalled conditional loop since loop is controlled by a conditionAlso called pretest loop since test has to pass before it does the instructions in loopOtherwise it skips those instructions
15 While Instruction Flowchart 5.2While Instruction FlowchartLoop’s condition is tested before each repetition of loopIf true it performs a repetition and starts overIf false the loop terminates
16 Tennis Racket ExampleTennis racket is rotated 0.06 revolutions until it is next to the ballBall then moves forward looking like it was hitUsesFunction callRelational operation
18 Inserting While Statement Drag While tile up to Method EditorChoose True or False as placeholder from popup menu
19 Vanishing Cookie Example Magician says AbracadabraIn While loopCookie reduces in opacity by 10% until opacity is 0% or cookie disappearsUsesOpacity propertyRelational operationUses math to changewhile condition
20 Shrinking Alice Example In Alice in Wonderland, Alice drinks something which causes her to shrink which allows her to go thru a small doorWhile loop is used to constantly shrink her by half until she is smaller than doorUsesTwo function callsRelational operation
21 Rescue Scuba Diver Example Helicopter moves to scuba diverWith While loop Helicopter descends to diver ½ meter at time until 1 meter above diverNotice style is abruptlyTo prevent jerky movementLadder is lowered to diver
22 Shark Chases Fish Example Common feature in popular "action films" is exciting chase sceneHungry shark chases after and catches a fleeing fish
23 Shark Chases Fish Problem How do we get shark to chase goldfish in a chase-like action?Shark should not immediately catch goldfishOtherwise there would be no chaseGoldfish should appear to be fleeing
24 Shark Chases Fish Solution To create a chase sceneShark will swim short distance toward fishAt same time fish will swim short distance away from sharkFish will flee to a random locationAs long as goldfish is 0.5 meter away from shark, repeat above actions
25 Stepwise Refinement Process of breaking problem into smaller tasks Then break each task into simpler stepsOnce storyboard completed write method for each task
26 Storyboard: Shark Chases Fish While the goldfish is more than 0.5 meters away from the sharkDo in ordershark point at the goldfishDo togethershark swim (toward the goldfish)goldfish flee (away from the shark)shark eat (the goldfish)shark swim, goldfish flee, and shark eat actions are complexUse stepwise refinement to break them down into simple steps
27 Chase Storyboards chase While the goldfish is more than 0.5 meters from the sharkDo in orderPoint the shark at the goldfishDo togethershark swimgoldfish fleeshark eat (goldfish)swimDo in orderturn torso left and move forwardturn torso right and move forwardfleeDo togetherwiggle tailmove to random locationeatParameter: whatDo in ordershark points at whatshark opens jaw and what disappearsshark closes jaw
28 Checkpoint Why is the While instruction considered a conditional loop? What causes the While loop to stop repeating?Why is the While loop called a pretest loop?
29 Nested Loops A nested loop is a loop that is inside of another loop The inner loop goes through all its iterations for every single iteration of the outer loopHow many times does bee pace or move forward?
30 Ferris Wheel Example Whole Ferris wheel will rotate clockwise Inner loop runs completely each time outer loop runs once
31 Ferris Wheel ExampleOuter loop executes 10 times and inner loop executes 2 timesHow many times does inner wheels rotate?Inner loop executed 20 times2 wheel inner rotations * 10 outer rotations
32 Homework Read chapter 5 Answer questions in handout Do lab assignments after handing in answered questionsDue one week after assigned with 1 week grace