Language: EN

programacion-bucles

What are loops

Loops are control structures that allow you to repeat a set of instructions several times. Along with conditionals, they are two of the most important tools when it comes to programming.

It’s clear that computers are fast. Very, very fast. But, precisely because of that, they wouldn’t be very useful if they were only able to do each thing once.

Imagine you have a system to check if a text, containing an ID card number, is correct. The place where computers excel is their ability to check millions and millions per second.

This is where the concept of a “loop” comes in. It is a structure that allows you to repeat an action multiple times, as long as a condition is met. The condition is an expression that evaluates to a boolean true or false.

As we can see, they have in common with conditionals that they both modify the flow of the program based on a condition. In fact, we will see at some point that, under the hood, a conditional and a loop are almost identical.

programacion-loop

Loops are fundamental in programming as they allow for automating tasks and performing repetitive operations efficiently.

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 also do frequently in our daily lives.

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

programacion-bucle-cartas

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

while remainingLetters:
	# take letter
    # put stamp

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 need to count how many letters we have stamped, and when we reach 1500, stop.

So the condition now 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 be something like this,

counter = 0

while counter < 1500:
	# take letter
    # put stamp
    counter = counter + 1

This would be an example of a WHILE + COUNTER loop. In fact, it is 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 stopping criterion. For example, if we make a mistake in the condition, and it always evaluates as TRUE, we will fall into an infinite loop.

For example, let’s look at the following loop.

while 1 < 2:
	# do something

Where we have messed up, and we have put a condition that is always TRUE. In this case, the program would enter the loop, and could never exit. This is called an infinite loop.

When we get into an infinite loop, our program gets stuck 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 obvious that it is wrong. But when you program, at some point you will make a less obvious mistake, 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 that will happen to you a thousand times in your life. You just have to pay attention so you know what’s happening. 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:

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

We must know them all, and use the one that best suits each situation, to improve the readability of our code.