Repetition Structures - PowerPoint PPT Presentation

1 / 17
About This Presentation
Title:

Repetition Structures

Description:

Repetition Structures Chapter 5 Part 2 5-* The While Instruction Combines Loop and the condition that you have seen be used in If/else statements Is a loop that ... – PowerPoint PPT presentation

Number of Views:129
Avg rating:3.0/5.0
Slides: 18
Provided by: Anthon224
Learn more at: https://www.kean.edu
Category:

less

Transcript and Presenter's Notes

Title: Repetition Structures


1
Repetition Structures
  • Chapter 5 Part 2

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
  • Otherwise it skips those instructions

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

4
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

6
Tennis Racket Flowchart
Start
  • Test loops condition is racket behind ball?
  • Yes? Turn racket forward 0.06 revolutions
  • No? Simulate racket hitting ball by moving
    forward 5 meters

Racket Distance Behind ball gt 0?
Yes
Racket turnsforward 0.06 revolution
No
Ball movesforward 5 meters
End
7
Object Slowly Disappears
  • Magician says Abracadabra
  • In While loop
  • Cookie reduces in opacity by 10 until opacity is
    0 or cookie disappears
  • Uses
  • Opacity property
  • Relational operation
  • Uses math to change
  • while condition
  • Notice Condition is changed in While

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 door
  • While loop is used to constantly shrink her by
    half until she is smaller than door
  • Uses
  • Two function calls
  • Relational operation
  • Notice Condition is changed in While

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
  • Notice style is abruptly
  • Prevents jerky movement
  • After While is finished ladder is lowered to
    diver
  • Event turns blades with infinite loops

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
  • Shark should not immediately catch goldfish
  • Otherwise there would be no chase
  • Goldfish should appear to be fleeing

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

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

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?

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?

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

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

17
Homework
  • Read chapter 5
  • Do lab assignments
  • Due in one week
Write a Comment
User Comments (0)
About PowerShow.com