1 / 16

Repeating Actions_2

Repeating Actions_2. What will we learn today?. Repeated Actions While loops in Real Life While loops How Do While Loops Work Example. Repeated Actions.

adelinad
Download Presentation

Repeating Actions_2

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. Repeating Actions_2

  2. What will we learn today? • Repeated Actions • While loops in Real Life • While loops • How Do While Loops Work • Example

  3. Repeated Actions • Last time we talked about repeated actions (loops), so if we need to repeat an action for a specific number of times, we just use a loop. But what if we don’t know the number of times an action should be repeated in advance?

  4. Repeated Actions with While • Consider these daily actions • When we walk towards the door, we usually don’t count the number of steps needed to reach there. We usually keep walking till we get there • When we color a picture, we don’t count the number of colored lines added to the picture. We simply keep on coloring until we see that it is fully colored

  5. While Loops • A while loop is a repetition of action(s) that stops if a condition is realized (while some condition is true perform instruction(s)) • While you didn’t reach the door, move a step forward • While the shape is not fully colored, draw a line with the colored pencil

  6. How Do While Loops Work? • The condition used in a while statement is a Boolean condition • If condition is true, instructions of the loop are executed • Else, the instructions are skipped • We keep performing the same action(s) till the condition becomes false No exit loop Yes Is Condition True Execute Instructions Perform Actions

  7. How do While Loops Work? Example No Exit Yes Is the shape NOT fully colored? Draw one line with a colored pencil

  8. Wolf and Bunny Example • Some day a wolf found a bunny in the garden and it hunted the bunny. The wolf kept running after the bunny till it cached it and ate it • The Storyboard Do in Order while the bunny is more than 0.5 m away from the wolf Do in Order wolf points at bunny Do Together wolf moves towards the bunny bunny moves away from the wolf Wolf eats the bunny

  9. Wolf and Bunny Example Is the bunny more than 0.5m away from the wolf ? NoWolf eats bunny Yes We will create two methods ‘run’ and ‘eat’ for the wolf and a method ‘flee’ for the bunny. In addition, a world-level method ‘chase’ will be developed * Wolf moves a step towards bunny * Bunny moves a step away from the wolf

  10. Implementing the Animation • Create a new method and call it “Chase” • Drag a while tile into the editor • Choose true as the initial condition • Click on the world-tab, choose a>b, replace it with true and choose 0.5 for a and 0.5 for b

  11. Implementing the Animation • Select the wolf tab and choose the function ‘wolf distance to’ from the functions tab and place it in first rectangle • Select ‘bunny’ from the list

  12. Implementing the Animation • Select the wolf object and create a new method “run” as follows

  13. Implementing the Animation • Select the bunny object and create a new method “flee” as follows

  14. Implementing the Animation • Select the bunny object and create a new method “eat” as follows

  15. The While Statement

  16. Let’s Work with Alice • A Guy Racing • Create a world as shown in the picture, with a coach, a runner and 2 speed limit signs • Write a method ‘run’ in which the runner keeps running till he reaches the sign • Use a while loop in the method (while the distance between the guy and the sign is > 1) keep running • The coach measures the runner’s speed with a stop watch, checks the time at the end and says ‘Excellent Timing’

More Related