Upload
philip-clark
View
219
Download
0
Embed Size (px)
Citation preview
Repetition StructuresChapter 5 Part 2
5-1
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
While Instruction Flowchart
Loop’s condition is tested before each repetition of loopo If true it performs a
set of instructions and loop starts over
o If false then the loop terminates
3
Inserting While Statement Drag While tile up to Method Editor
Choose True or False as placeholder from popup menu
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
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 DistanceBehind
ball > 0?
Racket turnsforward 0.06
revolution
Ball movesforward 5 meters
End
Yes
No
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 propertyo Relational operationo Uses math to
change while condition
Notice Condition is changed in While
7
Change Size in While In Alice in
Wonderland, Alice drinks something which causes her to shrink which allows her to go thru a small door
While loop is used to constantly shrink her by half until she is smaller than door
Useso Two function callso Relational
operation Notice Condition is
changed in While 8
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 divero Notice style is abruptly
Prevents jerky movement
After While is finished ladder is lowered to diver
Event turns blades with infinite loops
9
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
Shark Chases Fish Solution
To create a chase scene o Shark will swim short distance toward fisho At same time fish will swim short distance
away from sharko Fish will flee to a random locationo As long as goldfish is 0.5 meter away from
shark repeat above actions
11
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
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
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
Ferris Wheel Example Whole Ferris wheel will rotate
clockwise Inner loop runs completely
each time outer loop runs once
15
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
Homework Read chapter 5 Do lab assignments Due in one week
17