120 likes | 150 Views
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
E N D
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 • When we walk, we move our legs forward and backward as we move a counted number of steps
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??)
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
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
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
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
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
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