1 / 14

Ch 7 Loops

Ch 7 Loops. Alice has two control structures for controlling the repeated execution of statements Loop While. Text: A bunny sneaks into a garden and wants to eat the broccoli. The bunny will need to hop several times to get to the broccoli. Loop.

ricky
Download Presentation

Ch 7 Loops

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. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Ch 7 Loops • Alice has two control structures for controlling the repeated execution of statements • Loop • While ACS-1805 Ron McFadyen

  2. Text: A bunny sneaks into a garden and wants to eat the broccoli. The bunny will need to hop several times to get to the broccoli. Loop A definite loop is executed a specified number of times (also referred to as a counted loop) Also discussed back in Ch 3 ACS-1805 Ron McFadyen

  3. Flowchart for Loop Set index to initial value Loop action(s) executed as an index variable • starts at some initial value • is incremented by some increment before a next iteration • goes up to some limit Increment index index < limit? true Loop action(s) false ACS-1805 Ron McFadyen

  4. bunny.hop ACS-1805 Ron McFadyen

  5. bunny.hop vs Less code More flexible Easier to comprehend ACS-1805 Ron McFadyen

  6. Infinite Loop In an animation we may want for some activity to never stop, unless the world stops playing ACS-1805 Ron McFadyen

  7. Nested Loops FerrisWheel.a2w Each time the ferris wheel makes a revolution the smaller wheels must turn too The inner loop is executed twice (in this example) for each iteration of the outer loop ACS-1805 Ron McFadyen

  8. 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 don’t 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 ACS-1805 Ron McFadyen

  9. While ACS-1805 Ron McFadyen

  10. Text Example • The Shark/Goldfish Chase Scene. ACS-1805 Ron McFadyen

  11. 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. ACS-1805 Ron McFadyen

  12. 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. ACS-1805 Ron McFadyen

  13. 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 ACS-1805 Ron McFadyen

  14. SharkGoldfishChase.a2w 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 ACS-1805 Ron McFadyen

More Related