Monday, July 28, 2014

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  
·      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  
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.

No comments:

Post a Comment