repetition structures chapter 5 5-1. chapter 5 objectives the loop instruction the while instruction...

32
Repetition Structures Chapter 5 5-1

Upload: lynn-briggs

Post on 16-Dec-2015

229 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Repetition Structures Chapter 5 5-1. Chapter 5 Objectives The Loop Instruction The While Instruction Nested Loops 5-2

Repetition StructuresChapter 5

5-1

Page 2: Repetition Structures Chapter 5 5-1. Chapter 5 Objectives The Loop Instruction The While Instruction Nested Loops 5-2

Chapter 5 Objectives The Loop Instruction The While Instruction Nested Loops

5-2

Page 3: Repetition Structures Chapter 5 5-1. Chapter 5 Objectives The Loop Instruction The While Instruction Nested Loops 5-2

The Loop Instruction

Simple control structure allowso Instruction (or block of instructions) to be

repeated multiple timeso Repeated fixed amount of times

A whole number

Page 4: Repetition Structures Chapter 5 5-1. Chapter 5 Objectives The Loop Instruction The While Instruction Nested Loops 5-2

Flowcharting a Loop Instruction

5-4

Set index to 0

Index < 12?Clock, minute roll

left 1 revolution

Clock, hour roll

left 1/12th

revolution

Add 1 to index

T

5.1

F

Page 5: Repetition Structures Chapter 5 5-1. Chapter 5 Objectives The Loop Instruction The While Instruction Nested Loops 5-2

The Loop Instruction

Simplifies to:

5-5

5.1

Page 6: Repetition Structures Chapter 5 5-1. Chapter 5 Objectives The Loop Instruction The While Instruction Nested Loops 5-2

Using 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

5-6

5.1

Page 7: Repetition Structures Chapter 5 5-1. Chapter 5 Objectives The Loop Instruction The While Instruction Nested Loops 5-2

Computing Number of Times to Repeat What if you want something to loop and NOT have

to specify a particular number of repetitions?o Example: getting a ball to roll across into a goal

regardless of where the ball is initially placed

o Use the soccerBall’s distance to function to calculate distance to goal

o Loop uses truncation: decimal portions of numbers are discarded

o Examples: objects loop 4 times not 4.8 or 3 times not 3.4

5-7

5.1

Page 8: Repetition Structures Chapter 5 5-1. Chapter 5 Objectives The Loop Instruction The While Instruction Nested Loops 5-2

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

5-8

Page 9: Repetition Structures Chapter 5 5-1. Chapter 5 Objectives The Loop Instruction The While Instruction Nested Loops 5-2

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

Page 10: Repetition Structures Chapter 5 5-1. Chapter 5 Objectives The Loop Instruction The While Instruction Nested Loops 5-2

Infinite Loops

The loop that never stopso 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.

5-10

5.1

Page 11: Repetition Structures Chapter 5 5-1. Chapter 5 Objectives The Loop Instruction The While Instruction Nested Loops 5-2

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

5-11

Page 12: Repetition Structures Chapter 5 5-1. Chapter 5 Objectives The Loop Instruction The While Instruction Nested Loops 5-2

Infinite Loop Continual Motion

• Helicopter blades are turning while helicopter rescues diver

5-12

Page 13: Repetition Structures Chapter 5 5-1. Chapter 5 Objectives The Loop Instruction The While Instruction Nested Loops 5-2

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?

5-13

Page 14: Repetition Structures Chapter 5 5-1. Chapter 5 Objectives The Loop Instruction The While Instruction Nested Loops 5-2

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

5-14

5.2

Page 15: Repetition Structures Chapter 5 5-1. Chapter 5 Objectives The Loop Instruction The While Instruction Nested Loops 5-2

While Instruction Flowchart

Loop’s condition is tested before each repetition of loopo If true it

performs a repetition and starts over

o If false the loop terminates

5-15

5.2

Page 16: Repetition Structures Chapter 5 5-1. Chapter 5 Objectives The Loop Instruction The While Instruction Nested Loops 5-2

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

5-16

Page 17: Repetition Structures Chapter 5 5-1. Chapter 5 Objectives The Loop Instruction The While Instruction Nested Loops 5-2

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

5-17

5.2

Start

Racket Distance

Behind

ball > 0?

Racket turns

forward 0.06

revolution

Ball moves

forward 5 meters

End

Page 18: Repetition Structures Chapter 5 5-1. Chapter 5 Objectives The Loop Instruction The While Instruction Nested Loops 5-2

Inserting While Statement Drag While tile up to Method Editor

Choose True or False as placeholder from popup menu

5-18

Page 19: Repetition Structures Chapter 5 5-1. Chapter 5 Objectives The Loop Instruction The While Instruction Nested Loops 5-2

Vanishing Cookie Example 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 5-19

Page 20: Repetition Structures Chapter 5 5-1. Chapter 5 Objectives The Loop Instruction The While Instruction Nested Loops 5-2

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

Useso Two function callso Relational

operation5-20

Page 21: Repetition Structures Chapter 5 5-1. Chapter 5 Objectives The Loop Instruction The While Instruction Nested Loops 5-2

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 abruptlyo To prevent jerky

movement Ladder is lowered

to diver5-21

Page 22: Repetition Structures Chapter 5 5-1. Chapter 5 Objectives The Loop Instruction The While Instruction Nested Loops 5-2

Shark Chases Fish Example Common feature in popular "action films" is

exciting chase scene Hungry shark chases after and catches a fleeing

fish

Page 23: Repetition Structures Chapter 5 5-1. Chapter 5 Objectives The Loop Instruction The While Instruction Nested Loops 5-2

Shark Chases Fish Problem

How do we get shark to chase goldfish in a chase-like action?o Shark should not immediately catch goldfish

Otherwise there would be no chase

o Goldfish should appear to be fleeing

Page 24: Repetition Structures Chapter 5 5-1. Chapter 5 Objectives The Loop Instruction The While Instruction Nested Loops 5-2

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

Page 25: Repetition Structures Chapter 5 5-1. Chapter 5 Objectives The Loop Instruction The While Instruction Nested Loops 5-2

Stepwise Refinement Process of breaking problem into smaller

taskso Then break each task into simpler steps

Once storyboard completed write method for each task

Page 26: Repetition Structures Chapter 5 5-1. Chapter 5 Objectives The Loop Instruction The While Instruction Nested Loops 5-2

Storyboard: 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

Page 27: Repetition Structures Chapter 5 5-1. Chapter 5 Objectives The Loop Instruction The While Instruction Nested Loops 5-2

Chase Storyboardschase

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)

swim

Do in order

turn torso left and move forward

turn torso right and move forward

turn torso left and move forward

flee

Do together

wiggle tail

move to random locationeat

Parameter: what

Do in order

shark points at what

shark opens jaw and what disappears

shark closes jaw

Page 28: Repetition Structures Chapter 5 5-1. Chapter 5 Objectives The Loop Instruction The While Instruction Nested Loops 5-2

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?

5-28

Page 29: Repetition Structures Chapter 5 5-1. Chapter 5 Objectives The Loop Instruction The While Instruction Nested Loops 5-2

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?

5-29

Page 30: Repetition Structures Chapter 5 5-1. Chapter 5 Objectives The Loop Instruction The While Instruction Nested Loops 5-2

Ferris Wheel Example Whole Ferris wheel will rotate

clockwise Inner loop runs completely

each time outer loop runs once

Page 31: Repetition Structures Chapter 5 5-1. Chapter 5 Objectives The Loop Instruction The While Instruction Nested Loops 5-2

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

Page 32: Repetition Structures Chapter 5 5-1. Chapter 5 Objectives The Loop Instruction The While Instruction Nested Loops 5-2

Homework Read chapter 5 Answer questions in handout Do lab assignments after handing in

answered questions Due one week after assigned with 1 week

grace