Fall 2007ACS-1805 Ron McFadyen1 Ch 7 Loops Alice has two control structures for controlling the repeated execution of statements Loop While
Fall 2007ACS-1805 Ron McFadyen2 Loop A definite loop is executed a specified number of times (also referred to as a counted loop) Also discussed back in Ch 3, page 63+ 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.
Fall 2007ACS-1805 Ron McFadyen3 Flowchart for Loop index < limit? Loop action(s) false true 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 Set index to initial value Increment index
Fall 2007ACS-1805 Ron McFadyen4 bunny.hop
Fall 2007ACS-1805 Ron McFadyen5 bunny.hop vs Less code More flexible Easier to comprehend
Fall 2007ACS-1805 Ron McFadyen6 Infinite Loop In an animation we may want for some activity to never stop, unless the world stops playing
Fall 2007ACS-1805 Ron McFadyen7 Nested Loops 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