programacion-bucles

What are loops

  • 4 min

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’s clear that computers are fast (very, very fast). But, precisely because of that, they wouldn’t be very useful if they could only do each thing once.

Imagine you have a system to check if a text containing a DNI (Spanish ID number) is correct. Where computers excel is in their ability to check millions and millions per second.

This is where the concept of a “loop” comes in. It’s 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.

programacion-loop

Basic Loop

Loops are fundamental in programming because they allow us to perform repetitive tasks (which is what computers do best).

Loops have in common with conditionals that both modify the program flow based on a condition. In fact, we will see in due time that, under the hood, a conditional and a loop are basically almost identical.

Understanding a Loop

Understanding a loop is not much harder than understanding a conditional. In fact, performing loops of actions (repetitive tasks) is also something we do frequently in our daily lives.

For example, 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
  • While there are letters left in the pile, repeat the same sequence of actions.

programacion-bucle-cartas

While there are letters left, I take one and put a stamp

This would be an example of a WHILE loop. In this case, the condition is “are there more letters?” and the loop action is “take letter and put stamp”. Expressed in code it would be something like this.

while quedanCartas:
	# take letter
    # put stamp
Copied!

Another example of a loop could be,

I have to put a stamp on 1500 letters

This time we don’t care if there are more letters in the pile. In this case we must count how many letters we have stamped, and stop when we reach 1500.

So the condition now would be “have I put less than 1500 stamps?” and the action would be the same “take letter and put stamp”. For which, we have to count how many letters we have stamped previously. This, expressed in code, would be something like this,

contador = 0

while contador < 1500:
	# take letter
    # put stamp
    contador = contador + 1
Copied!

This would be an example of a WHILE loop + COUNTER. In fact, it’s such a common structure that most languages have a specific loop to do this action, which we call a FOR loop.

Stop Condition

Probably the most important point of a loop is the condition, or stop 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, look at the following loop.

while 1 < 2:
	# do something
Copied!

Where we messed up, and put 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 enter an infinite loop, our program gets trapped in it without being able to exit. It will keep consuming CPU, until we force close the program, or restart the computer.

Obviously, in the example the condition is very clearly wrong. But when you program, at some point you will make a less obvious error, which will cause your program to fall into an infinite loop.

Don’t worry. It’s something that has happened to all of us, and will happen to you a thousand times in your life. You just have to be aware so you know what’s happening to you (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 ones are the following:

Of all of them, the WHILE loop is the most basic one. In fact, the others are variations of this one. It’s “the father of all loops”.