Loops are control structures that allow repeating a set of instructions multiple times. Along with conditionals, they are two of the most important tools when programming.
It is clear that computers are fast (very very fast). But, precisely for that reason, they wouldn’t be very useful if they could only perform each task once.
Imagine you have a system to check if a text containing an ID number is correct. The area where computers excel is with their ability to check millions and millions per second.
This is where the concept of “loop” comes in. It is a structure that allows repeating an action multiple times, as long as a condition is met. The condition is an expression that evaluates to a boolean true
or false
.
Loops are fundamental in programming as they allow us to perform repetitive tasks (which is what computers do best).
Loops have in common with conditionals that both modify the flow of the program based on a condition. In fact, we will see in due time that, under the hood, a conditional and a loop are basically identical.
Understanding a loop
Understanding a loop is not much more difficult than understanding a conditional. In fact, performing loops of actions (repetitive tasks) is something we frequently do in our daily lives.
For example, let’s imagine you have to do this:
I have to put a stamp on each letter in this pile
This basically means,
- Take a letter, put a stamp on it
- As long as there are letters left in the pile, repeat the same sequence of actions.
This would be an example of a loop WHILE. In this case, the condition is “Are there more letters?” and the action of the loop is “take a letter and put a stamp on it”. Which expressed in code would look something like this.
while quedanCartas:
# take letter
# put stamp
Another example of a loop could be,
I have to put a stamp on 1500 letters
In this case, we don’t care if there are more letters in the pile. Here we must count how many letters we have stamped, and when we reach 1500, we stop.
So now the condition would be “Have I put less than 1500 stamps?” and the action would be the same “take a letter and put a stamp on it”. For which, we have to count how many letters we have stamped previously. This, expressed in code would look something like this,
contador = 0
while contador < 1500:
# take letter
# put stamp
contador = contador + 1
This would be an example of a loop WHILE + COUNTER. In fact, it is such a common structure that most languages have a specific loop for performing this action, which we call loop FOR.
Stopping condition
Probably the most important point of a loop is the condition, or stopping criterion. For example, if we make a mistake in the condition, and it always evaluates to TRUE
, we will fall into an infinite loop.
For example, let’s look at the following loop.
while 1 < 2:
# do something
Where we’ve messed up by putting a condition that is always TRUE
. In this case, the program would enter the loop and could never exit (we call this an infinite loop).
When we fall into an infinite loop, our program gets stuck in it without being able to exit. It will continue consuming CPU, until we force close the program or restart the computer
Evidently, in the example, the condition is very clearly wrong. But when you program, at some point you will make a less obvious error that will cause your program to fall into an infinite loop.
Don’t worry. It’s something that has happened to all of us, and it will happen to you a thousand times in your life. You just have to be alert so you know what’s happening to you (fix it, and move on).
Types of loops
There are several types of loops in programming, each with its own characteristics and uses. The most common are as follows:
Of all of them, the WHILE loop is the most basic of them all. In fact, the others are variations of this. It is “the parent of loops”.