Ch 7 Loops - PowerPoint PPT Presentation

1 / 8
About This Presentation
Title:

Ch 7 Loops

Description:

Generally, we expect a while to terminate when something in the world causes the ... While the goldfish is more than 0.5 meters away from the shark. Do in order ... – PowerPoint PPT presentation

Number of Views:48
Avg rating:3.0/5.0
Slides: 9
Provided by: wand85
Category:
Tags: loops

less

Transcript and Presenter's Notes

Title: Ch 7 Loops


1
Ch 7 Loops
  • Alice has two control structures for controlling
    the repeated execution of statements
  • Loop
  • While

2
While
A while loop is executed as long as some
condition is true also called a conditional
loop or indefinite loop) Used in situations where
we dont know how many times a loop should
execute, but we do know the condition for it to
execute. Generally, we expect a while to
terminate when something in the world causes the
expression to be false. However, it could be
coded such that it executes forever an infinite
loop
3
While
4
Text Example
  • The Shark/Goldfish Chase Scene.

5
Problem
  • The problem is how do we get the shark to chase
    the goldfish in a chase-like action?
  • The shark should not immediately catch the
    goldfish (otherwise, there would be no chase).
  • The goldfish (assuming self-preservation
    instincts) should appear to be fleeing.

6
Solution
  • To create a chase scene,
  • At the same time, the shark will swim a short
    distance toward the fish and the fish will swim a
    short distance away from the shark.
  • The fish will flee.
  • As long as the goldfish is still 0.5 meters
    away from the shark, repeat the actions.

7
Storyboard
  • 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)

Complex actions become procedures themselves
8

chase 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)

flee Do together wiggle tail move to random
location
swim tDo in order urn torso left and move
forward turn torso right and move forward
turn torso left and move forward
Eat Parameter what Do in order shark
points at what shark opens jaw and what
disappears shark closes jaw
Write a Comment
User Comments (0)
About PowerShow.com