1 / 12

Repeating Actions

Repeating Actions. What will we learn today?. Repeated Actions Need for Repetition Repetition with Loop Nested Loops. Repeated Actions in Real Life. Consider those daily actions When we talk, we simply open and close our months repeatedly a counted number of times

bravom
Télécharger la présentation

Repeating Actions

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. What will we learn today? • Repeated Actions • Need for Repetition • Repetition with Loop • Nested Loops

  3. Repeated Actions in Real Life • Consider those daily actions • When we talk, we simply open and close our months repeatedly a counted number of times • When we walk, we move our legs forward and backward as we move a counted number of steps

  4. Need for Repetition • Consider a situation in which a man walks 5 steps forward towards a palm tree • The man’s legs walk a step with each meter that he moves forward (if man moves forward 5 meters, his legs walking occur 5 times) • One possible way is to animate 5 steps • But, what if we want to repeat the walking action 20 times (becomes tedious and boring, isn’t it??)

  5. Repetition with Loops • An easy way to do this is by using a loop statement • The Loop Statement • Is a simple way to repeat an action a counted number of times • Is used to execute a block of instructions again and again (limited number of times) • Is quick and easy to write and also easy to understand • A count is used to describe the number of times a loop/action(s) repeats

  6. Hide and Seek Example with Loops • Remember the “hide and seek” example we did in the methods lecture • Remember how many times we had to animate(copy and past) the actions • Painful and tedious wasn’t it!!! • Now, the torture is over and we will use loops to make our lives simpler

  7. Repetition with Loop • We have repeated actions in ‘Turn Around’ methods and in the ‘world.myfirst’ method • To create a loop in a program • Drag a loop tile into the editor inside the do-in order block of ‘world.myfirst’ method • Choose other and enter number 3 in the popup menu • Drag the ‘world.seekCat’ method into the loop statement • Inside the do-together block make two loops for the turning around methods

  8. Nested Loops • A loop can be nested inside another loop (a loop is placed inside other loop) • Remember, the ‘move around’ method was used to make a complete turn around the tree (which requires repeating the moving a step and turning right actions ten times) • To make 3 complete turns we need to repeat the previous action 3 times Loop 3 times Loop 10 times Move Around

  9. Nested Loop • The second loop ‘Loop 10 times’ is called a nested loop • It will fully execute each time the outer loop ‘loop 3 times’ executes once • The count of the outer loop is 3, and the count of the inner loop is 10 • The inner loop will execute 10 times for each of the 3 executions of the outer loop • The moving around action will occur 30 (3*10) times in total Loop 10 times Move Around

  10. The Result Loop Statement

  11. Turn Around methods with Loops

  12. Let’s Work with Alice • Write an animation where an Arabian man in his tent serves coffee to his guests • Create a method ‘fill-inCups’ in which the host man holding the dallah (middle east) and fill-in 3 empty cubs in the tray (loop 3 times) • Create a method ‘serveCoffee’ where the host man serves his guests (loop 2 times) • Develop the animation for this story

More Related