repetition structures
Skip this Video
Download Presentation
Repetition Structures

Loading in 2 Seconds...

play fullscreen
1 / 32

Repetition Structures - PowerPoint PPT Presentation

  • Uploaded on

Repetition Structures. Chapter 5. Chapter 5 Objectives. The Loop Instruction The While Instruction Nested Loops. The Loop Instruction. Simple control structure allows Instruction (or block of instructions) to be repeated multiple times Repeated fixed amount of times A whole number.

I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
Download Presentation

PowerPoint Slideshow about 'Repetition Structures' - rachel-puckett

An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.

- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
chapter 5 objectives
Chapter 5 Objectives
  • The Loop Instruction
  • The While Instruction
  • Nested Loops
the loop instruction
The Loop Instruction
  • Simple control structure allows
    • Instruction (or block of instructions) to be repeated multiple times
    • Repeated fixed amount of times
      • A whole number
flowcharting a loop instruction
5.1Flowcharting a Loop Instruction

Set index to 0

Index < 12?


Clock, minute rollleft 1 revolution

Clock, hour rollleft 1/12threvolution

Add 1 to index


using loop instruction
5.1Using Loop Instruction
  • Drag Loop instruction from bottom of Method Editor
  • It prompts you for amount of times to loop
  • Creates empty slot where instructions can be placed
computing number of times to repeat
5.1Computing Number of Times to Repeat
  • What 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 placed
    • Use the soccerBall’s distance to function to calculate distance to goal
    • Loop uses truncation: decimal portions of numbers are discarded
    • Examples: objects loop 4 times not 4.8 or 3 times not 3.4
loops controlled by variable
Loops Controlled by Variable
  • Ask user for amount of times a lion should roar
  • Place value in variable
  • Loop the amount of times user entered
loop with function call
Loop With Function Call
  • Loop count can be computed by calling a function that returns a number value
  • Loop instruction automatically rounds returned value down to whole number
infinite loops
5.1Infinite Loops
  • The loop that never stops
    • Use for objects that shouldn’t stop
  • If 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.
infinite loop background example
Infinite Loop Background Example
  • Infinite loops are good for moving objects in background
  • This example simulates moving water by changing texture of water
  • Called from main method
infinite loop continual motion
Infinite Loop Continual Motion
  • Helicopter blades are turning while helicopter rescues diver
  • 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?
the while instruction
5.2The While Instruction
  • Combines Loop and If/else statements
  • Is a loop that repeats as long as its Boolean condition is true
  • Called conditional loop since loop is controlled by a condition
  • Also called pretest loop since test has to pass before it does the instructions in loop
    • Otherwise it skips those instructions
while instruction flowchart
5.2While Instruction Flowchart
  • Loop’s condition is tested before each repetition of loop
    • If true it performs a repetition and starts over
    • If false the loop terminates
tennis racket example
Tennis Racket Example
  • Tennis racket is rotated 0.06 revolutions until it is next to the ball
  • Ball then moves forward looking like it was hit
  • Uses
    • Function call
    • Relational operation
tennis racket flowchart
5.2Tennis Racket Flowchart


  • Test loop’s condition: is racket behind ball?
    • Yes? Turn racket forward 0.06 revolutions
    • No? Simulate racket hitting ball by moving forward 5 meters

Racket Distance


ball > 0?

Racket turnsforward 0.06


Ball movesforward 5 meters


inserting while statement
Inserting While Statement
  • Drag While tile up to Method Editor
  • Choose True or False as placeholder from popup menu
vanishing cookie example
Vanishing Cookie Example
  • 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

shrinking alice example
Shrinking Alice Example
  • 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
rescue scuba diver example
Rescue Scuba Diver Example
  • Helicopter moves to scuba diver
  • With While loop Helicopter descends to diver ½ meter at time until 1 meter above diver
  • Notice style is abruptly
    • To prevent jerky movement
  • Ladder is lowered to diver
shark chases fish example
Shark Chases Fish Example
  • Common feature in popular "action films" is exciting chase scene
  • Hungry shark chases after and catches a fleeing fish
shark chases fish problem
Shark Chases Fish Problem
  • How do we get shark to chase goldfish in a chase-like action?
    • Shark should not immediately catch goldfish
      • Otherwise there would be no chase
    • Goldfish should appear to be fleeing
shark chases fish solution
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
stepwise refinement
Stepwise Refinement
  • Process of breaking problem into smaller tasks
    • Then break each task into simpler steps
  • Once storyboard completed write method for each task
storyboard shark chases fish
Storyboard: Shark Chases Fish


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
chase storyboards
Chase Storyboards


While the goldfish is more than 0.5 meters from the shark

Do in order

Point the shark at the goldfish

Do together

shark swim

goldfish flee

shark eat (goldfish)


Do in order

turn torso left and move forward

turn torso right and move forward

turn torso left and move forward


Do together

wiggle tail

move to random location


Parameter: what

Do in order

shark points at what

shark opens jaw and what disappears

shark closes jaw

  • 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?
nested loops
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 loop
  • How many times does bee pace or move forward?
ferris wheel example
Ferris Wheel Example
  • Whole Ferris wheel will rotate clockwise
  • Inner loop runs completely each time outer loop runs once
ferris wheel example1
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
  • Read chapter 5
  • Answer questions in handout
  • Do lab assignments after handing in answered questions
  • Due one week after assigned with 1 week grace