Showing posts with label Introducing Repeat Loops to kids. Show all posts
Showing posts with label Introducing Repeat Loops to kids. Show all posts

Monday, July 28, 2014

Repeat Loops: Examples from your daily life

Before we start writing code for Pro-Bot using Repeat Loops, let’s go through a few examples from your daily life. We shall write a few Repeat Loops in plain English to get some practice with identifying repeating patterns & end conditions.


For each of the questions below, write a Repeat Loop in plain English. You need to perform the following steps for each:
1.   Identify the actions or patterns that repeat.
2.   Identify the loop ending condition. This can be:
  •  the number of times that the pattern repeats or
  • a specific condition that needs to be satisfied for the loop to stop.




Question 1
You decide to run 10 laps in the school cardio club today. You run around the field once for each lap. Can you write a Repeat Loop for this activity?

Action that repeats:


Loop ending condition:


Repeat  _________   [


]


Question 2
You decide to run 10 laps in the school cardio club today. At the end of running each lap, you scan a card that tracks the number of laps. Can you write a Repeat Loop for this activity?

Actions that repeat:




Loop ending condition:


Repeat  _________   [




]


Question 3
You reached school a bit after the cardio club started today. So, instead of running 10 laps, you decide to run till the first bell rings. Each time, you run around the field once and then scan a card that tracks the number of laps. Can you write a Repeat Loop for this activity?

Actions that repeat:




Loop ending condition:


Repeat  ____________________________________________________   [




]


Question 4
You are going to the zoo with your family. You start from home at 9.00 am. It takes 40 minutes to drive from your home to the zoo. We will assume there are no traffic delays. You are really excited and can’t wait to get there. 10 minutes after the car starts, you ask “Are we there yet?”. Every ten minutes thereafter, you keep asking “Are we there yet?”, until you reach the destination at 9.40 am. (You ask the question at 9.40 am too, you haven’t found a parking space yet.)  Can you write a Repeat Loop for this?


Action that repeats:



To find out how many times the action repeats, let’s write down the times at which you ask “Are we there yet?”

______________ am

______________ am

______________ am

______________ am


Loop ending condition:


Repeat  _________   [



]



Question 5
You are going to the zoo again with your family. As usual, you are really excited and can’t wait to get there. But this time, there is a lot of traffic delay due to some construction work on the freeway. 10 minutes after the car starts, you ask “Are we there yet?”. Every ten minutes thereafter, you keep asking “Are we there yet?”, until you reach the destination. You know that you have reached the zoo when you see the huge “ZOO” sign by the road. At soon as you see the sign, you stop asking “Are we there yet?”. Can you write a Repeat Loop for this case?


Action that repeats:



Loop ending condition:




Repeat ______________________________________________________ [



]



Question 6
You are hungry and decide to eat some grapes for a snack. You eat only one grape at a time. You stop eating as soon as your stomach feels full. Can you write a Repeat Loop for this activity?


Action that repeats:



Loop ending condition:



Repeat ______________________________________________________ [



]



Question 7
You have to print the pattern ABABABABAB. Can you write a Repeat Loop for this activity?


Action that repeats:


Loop ending condition:


Repeat  _________   [



]





Question 8
It's Halloween. You go from house to house, trick or treating. You get 2 candies from each house that you go to. Your mom has told you to return home once you collect 100 candies. Can you write a Repeat Loop for this activity?


Action that repeats:



Loop ending condition:



Repeat ________ [



]

Question 9
It's Halloween. You go from house to house, trick or treating. You have a big basket that you intend to fill up with candies. Your mom has told you to return home once your basket is full. Can you write a Repeat Loop for this activity?


Action that repeats:



Loop ending condition:



Repeat  ______________________________________________________ [



]

Repeat Loops in Pro-Bot

Let’s look at an example from one of your school activities.
You run laps in your PE class, don’t you? On some days, you run 1 lap, other days you run 2 or more laps and some days you don’t run any. If you look at this activity, you are repeating one action multiple times: you run a lap around the field 0, 1 or more times. You are running in a loop… A loop in computer programming is quite similar.


Repeat Loop in computer programming is a set of instructions that is continually repeated until a certain condition is satisfied.


When do we use Repeat Loops?
We use Repeat Loops when an action (or a set of actions) is repeated multiple times.

Two steps are required to write a Repeat Loop:
(1) We need to identify the action (or a set of actions) that gets repeated
(2) We need to know how to end the repetition. It could be either:
·      the number of times that the action gets repeated
·      the condition that needs to be satisfied for the Repeat Loop to stop executing.

(Note: If you do not specify either of the above, the loop can run forever. Hence it is very important that you identify the ending conditions for the repeat loop.)


Repeat Loops can be of the following two types:

1.   Repeat Loops where the action repeats for a specific number of times.
Here, you need to know the number of times to repeat before you write your loop. The computer will count each time you perform the action and when the count reaches the number of times that you specified, it will stop executing the loop.

Example: You decide to run 5 laps in your PE class today. The action that gets repeated is “run one lap around the field”. You know that you need to repeat this action 5 times. When you start, your count is 0. Each time you finish running a lap and get to the start point, you add a 1 to the number of laps that you have completed. When you have finished running 5 laps, your count is 5 and you stop at this point.

2.   Repeat Loops where the action repeats till a specified condition is satisfied.
Here, you do not need to know the exact number of times to repeat the action; but you need to check at the end of each repeat cycle to see if some specified condition is satisfied or not. If the condition is satisfied, the computer stops executing the loop. If not, the action is repeated.

Example: Let’s continue with our PE class example. You decide to run laps continuously till the  bell rings. In this case, you do not need to know ahead how many laps you are going to run. You run your first lap. When you get to the start point, you check to see if the bell has rung. If yes, you stop running. If not, you run another lap, get to the start point and again check if the bell has rung. You continue this till the bell rings. The number of times that you run can be different on different days with this method. If the bell rang before you even started your first lap, you do not run at all.



Now, let’s talk about using Repeat Loops in Pro-Bot…
Pro-Bot allows us to use only one kind of Repeat Loop – the one where the action repeats for a specific number of times.

To create a Repeat Loop in your Pro-Bot program, first press the Rpt[ key. Then specify the number of times you want your instructions to repeat. Next, write the set of instructions that need to be repeated. End the loop with the closing bracket ].

Note:   If you do not specify the number of times to repeat, Pro-Bot will repeat the action 255 times. This helps avoid infinite loops; if not, Pro-Bot would have repeated those actions for ever (or until the battery ran out).

Example: You write a Repeat Loop for Pro-Bot to repeat “Fd 10, Rt” 12 times.
This is how the code will appear on Pro-Bot’s LCD screen.
Rpt 12 [
Fd 10  
Rt
]
·      When you press the GO button, Pro-Bot will repeat “Fd 10, Rt” 12 times.
·      If you forget to put in the number 12; ie; your program looks like
Rpt [
Fd 10  
Rt
]
Pro-Bot will repeat “Fd 10, Rt” 255 times.



Why do we use Repeat Loops instead of writing the repeating sequence of instructions several times?

1.   Repeat Loops make it easy for us to change the number of times to repeat the instructions inside the loop. This allows us to reuse the code inside our repeat loop and avoid rewriting it.
For example, in your cardio club loop, you can change the total number of times that you run without making any change to the process of running each lap.
2.   Repeat Loops make it easier for us to read and understand our programs.
3.   Repeat Loops also help to reduce the size of our programs.
     If you look at the above example, the loop is just 4 lines of code, but writing the program without the loop would take 12 repetitions of “Fd 10, Rt”; ie; 24 lines of code.