The FOR loop is a control structure that allows you to repeat a block of code a specific number of times. It is one of the most widely used and common control structures.
We have already seen the WHILE and DO-WHILE loops, which are the most basic loops.
As loops were used, it often happened that there was a need to do something N times (100, 1,000, 1,000,000). Without depending on a condition, simply
I want to do this 100 times
This could be done with a simple WHILE loop. For example, if I wanted to perform an action 100 times, we could use a counter and do:
counter = 0
while(counter < 100)
{
// do something
counter += 1;
}
But this structure was repeated so often that they said, “hey, what if we do something to make it easier?“. And thus the FOR loop was born.
Ultimately, the FOR loop they were looking for was something like this:
for(100)
{
// do something
}
Which in diagram form would look something like this:
Evolution of FOR loops
In reality, a FOR loop like the one I’ve given you has never been implemented that way. The first versions of the FOR loop were found in 1960 with languages like ALGOL.
Here the loop was defined as:
for i := 1 step 1 until 10 do
write(i);
Where we say that we want to count from 1 to 10, in increments of 1. These options allowed the FOR loop to be more powerful and versatile.
The declaration of FOR loops evolved to make it more adaptable, leading to the most widespread syntax, which we find today in many languages like C:
for(initialization; condition; update)
{
// do something
}
In this FOR loop, as it is defined in many languages, consists of three parts.
initialization
, a statement that is executed before entering the loopupdate
, statement that is executed in each iteration of the loopcondition
, statement that is evaluated to continue the loop
Which would be equivalent to the WHILE loop:
initialization;
while(condition) {
// do something
update;
}
Examples of FOR loops in different languages
Let’s look at examples of FOR loop syntax in different languages.
In the “descendant” languages of C, C++, C#, or Java, the syntax of a FOR loop is as follows
for (int i = 0; i < 100; i++)
{
// do something
}
In JavaScript, the syntax of the FOR loop is identical. The only change is the declaration of the variable i
, which in this case is done with the reserved word let
:
for (let i = 0; i < 100; i++)
{
// do something
}
On the other hand, Python does not have a FOR loop as such. It only has WHILE and FOR EACH, which act on iterable collections. To emulate the result of a FOR, one must use one of these structures, for example like this:
for i in range(100):
# do something
In these examples,
initialization
, we create a temporary variable called iupdate
, each iteration, we increment i with i++condition
, we perform the loop while i < 100
So we are performing the actions of the function body 100 times, which is the number of iterations it will take for i
to stop being i < 100
.
Be careful, when counting from 0, we must stop at 99. That is, when i < 100. It’s common to mess this up at first.
The FOR loop is one of the most popular and used in programming. To a large extent, due to the significant influence that C++ has had in the programming world.
However, for practical purposes, it is not as useful and powerful as other loops like WHILE or FOREACH. In fact, we have seen that languages like Python completely omit it (and live perfectly without it).
Moreover, although it arose to improve syntax, the fact that it actually groups three statements (initialization
, condition
, and update
) into a single structure can be somewhat confusing to explain to those who are just starting to program.
Good practices Tips
One of the biggest problems with FOR loops is the incorrect use that has frequently been made of it. Having three statements, which are also optional, gives a lot of freedom. But it also allows for “very original” but not very clean practices.
For example, in the past, leaving only the conditional to create a WHILE loop:
for (;condition;)
{
}
And even omitting all three statements to achieve an infinite loop, from which you can only exit with a break
.
Don’t do that, it’s a mess, use each loop for what it is.
Another common flaw is using complicated statements. For example, to update the index:
for (initialization;condition; i = i < 120 ? i * 3 + 4 : i + 2)
{
}
Or in the condition:
for (initialization; veryComplexValidation(); i++)
{
}
Don’t do any of those things, they are messy Keep the three statements as simple as possible.
In general, the usual structure is the only one you should use. If you need to perform some operation on the index, do it inside the loop:
for (var i = 0; i < N; i++)
{
var index = getIndex(i);
}
And if you need to evaluate a very complex condition, you probably should be using a WHILE loop.