Using Repetition in Your Code

Updated: January 25, 2022
No Credit Card Required


Practicing baseball pitches over and over can be tiring. At first, they are fast and accurate. But when you tire the pitches come in slower and off target. This can occur when people have to repeat the same exact action or movement repeatedly for a long time. This is called repetition.

Repetition can mean doing a singular action several times, it can also be more complex, like repeating a sequence of tasks again and again. The problem with repetition is the person loses focus over time, and the quality of the action gets worse. An automatic pitching machine, however, keeps firing ball after ball from the machine non stop at the same speed and velocity over and over. How does it do that? The secret is in the automatic pitching program.

To make the machine fire balls, we need to break down the action the pitcher has to do. One, he takes the ball and holds it firm. Two, he takes position to throw it and three, he swings the ball forward. These three steps are what we call a pattern. And what we just did was a pattern identification. A pattern means that a set of actions is being repeated in the same order for the same amount of time. And if we can identify a pattern, then we can easily create a robot to execute those tasks. Here, the automatic picture is doing just that. Computers have no feelings, and they don't get tired like us, they can repeat a process continuously giving the same result.

In a computer program repetition is achieved through the use of a loop. A loop executes an instruction repeatedly, so the computer programmer doesn't need to. If the program told the computer to execute the loop 20 times how many pitches would it throw? That's right 20 pitches, we would use a conditional statement to tell the computer to execute the loop 20 times but not 21.

A conditional statement is usually a small equation like if “A” is greater than “B”, then stop. We call this an “if-then” statement. If the number of pitches thrown is less than 20, then pitch one more time.

We can also use more than one conditional statement to control a loop. For instance, if he will get picked up at 6pm, he needs the automatic pitcher to stop pitching at 6pm. To do this, he adds a second condition to the original statement. If the number of pitches is less than or equal to 20 and time is less than 6pm. Then pitch one more time. The loop first checks the number of pitches it has already done to see if it has pitched less than 20 pitches, and then it checks the time to see if it is earlier than 6pm. If the number is less than 20 pitches, and it is not yet 6pm Then the automatic pitcher will pitch. In this case, we say both conditions are true.

What if the machine has thrown 18 pitches and the time is 6:05. In this case, the automatic pitcher will not throw a pitch. Why? Because it is after 6pm and one of the conditions is false. This is why conditional statements are so useful in programming. It gives a great flexibility to the programmer, you can use more than one condition and decide whether you want all of them to be true. None of them to be true, or even only a few of them.

As large complex computer programs can contain 1000s of lines of code, repetition through the use of loops is absolutely necessary. Otherwise, the programs would be too simple, too slow, unresponsive or ineffective. Or programmers would be old and gray before they ever finished even one program. Thank heavens for loops. And thank heavens for pitching machines. This team is sure to win more baseball games now. That's something that's definitely worth repeating.