160 likes | 183 Views
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.
E N D
What will we learn today? • Repeated Actions • While loops in Real Life • While loops • How Do While Loops Work • Example
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?
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
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
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
How do While Loops Work? Example No Exit Yes Is the shape NOT fully colored? Draw one line with a colored pencil
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
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
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
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
Implementing the Animation • Select the wolf object and create a new method “run” as follows
Implementing the Animation • Select the bunny object and create a new method “flee” as follows
Implementing the Animation • Select the bunny object and create a new method “eat” as follows
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’