Presentation is loading. Please wait.

Presentation is loading. Please wait.

Repetition Structures Chapter 5 Part 2 5-1. The While Instruction  Combines Loop and the condition that is also used in If/else statements  The loop.

Similar presentations


Presentation on theme: "Repetition Structures Chapter 5 Part 2 5-1. The While Instruction  Combines Loop and the condition that is also used in If/else statements  The loop."— Presentation transcript:

1 Repetition Structures Chapter 5 Part 2 5-1

2 The While Instruction  Combines Loop and the condition that is also used in If/else statements  The loop repeats as long as its Boolean condition is true  Called conditional loop since loop is controlled by a condition  Also called pretest loop since a test has to pass before it does the instructions in the loop o Otherwise it skips those instructions 2

3 While Instruction Flowchart Loop’s condition is tested before each repetition of loop o If true it performs a set of instructions and loop starts over o If false then the loop terminates 3

4 Inserting While Statement Drag While tile up to Method Editor Choose True or False as placeholder from popup menu 5

5 Proximity Example  Tennis racket is rotated 0.06 revolutions until it is next to the ball  Ball moves forward looking like it was hit  Uses  Function call  Relational operation 5

6 Tennis Racket Flowchart Test loop’s condition: is racket behind ball? o Yes? Turn racket forward 0.06 revolutions o No? Simulate racket hitting ball by moving forward 5 meters 6 Start Racket Distance Behind ball > 0? Racket turns forward 0.06 revolution Ball moves forward 5 meters End Yes No

7 Object Slowly Disappears Magician says Abracadabra In While loop o Cookie reduces in opacity by 10% until opacity is 0% or cookie disappears Uses o Opacity property o Relational operation o Uses math to change while condition Notice Condition is changed in While 7

8 Change Size in While In Alice in Wonderland, Alice drinks something which causes her to shrink which allows her to go thru a small doorAlice drinks While loop is used to constantly shrink her by half until she is smaller than door Uses o Two function calls o Relational operation Notice Condition is changed in While 8

9 Use While to Move an Object Helicopter moves to scuba diver In While loop Helicopter descends to diver ½ meter at time until 1 meter above diver o Notice style is abruptly  Prevents jerky movement After While is finished ladder is lowered to diver Event turns blades with infinite loops 9

10 Shark Chases Fish Example  Common feature in popular "action films" is an exciting chase scene  Hungry shark chases after and catches a fleeing fish o Shark should not immediately catch goldfish  Otherwise there would be no chase o Goldfish should appear to be fleeing 10

11 Shark Chases Fish Solution To create a chase scene o Shark will swim short distance toward fish o At same time fish will swim short distance away from shark o Fish will flee to a random location o As long as goldfish is 0.5 meter away from shark repeat above actions 11

12 Algorithm : Shark Chases Fish chase While the goldfish is more than 0.5 meters away from the shark Do in order shark point at the goldfish Do together shark swim (toward the goldfish) goldfish flee (away from the shark) shark eat (the goldfish) shark swim, goldfish flee, and shark eat actions are complex Use stepwise refinement to break them down into simple steps 12

13 Review Why is the While instruction considered a conditional loop? What causes the While loop to stop repeating? Why is the While instruction called a pretest loop? 13

14 Nested Loops A nested loop is a loop that is inside of another loop The inner loop executes all its iterations for every single iteration of the outer loop How many times does the bee pace or move forward? 14

15 Ferris Wheel Example Whole Ferris wheel will rotate clockwise Inner loop runs completely each time outer loop runs once 15

16 Ferris Wheel Example Outer loop executes 10 times and inner loop executes 2 times How many times does inner wheels rotate? o Inner loop executed 20 times  2 wheel inner rotations * 10 outer rotations 16

17 Homework Read chapter 5 Do lab assignments Due in one week 17


Download ppt "Repetition Structures Chapter 5 Part 2 5-1. The While Instruction  Combines Loop and the condition that is also used in If/else statements  The loop."

Similar presentations


Ads by Google