programacion-bucle-for

The FOR Loop

  • 5 min

The FOR loop is a control structure that allows repeating a block of code a specific number of times. It is one of the most used and frequent control structures.

As loops were used, it very frequently 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

That could be done with a WHILE loop easily. For example, if we wanted to perform an action 100 times we could use a counter and do:

counter = 0
while(counter < 100)
{
    // do something
    counter += 1;
}
Copied!

But this structure was repeated so, so often, that they said “hey, what if we make something to make it easier?”. And thus the FOR loop emerged.

Ultimately, the FOR loop they were looking for was something like this:

for(100)
{
	// do something
}
Copied!

Which in a diagram would look something like this:

programacion-bucle-for

FOR Loop Diagram

Evolution of FOR Loops

Actually, a FOR loop like the one I showed you has never been implemented that way. The first versions of the FOR loop are found in 1960 with languages like ALGOL.

Here the loop was defined as:

for i := 1 step 1 until 10 do
	write(i);
Copied!

Where we say 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, arriving at the most widespread syntax, which we find today in many languages like C:

for(initialization; condition; update)
{
	// do something
}
Copied!

In this FOR loop, as it is defined in many languages, it consists of three parts.

  • initialization, a statement that is executed before entering the loop
  • update, a statement that is executed in each iteration of the loop
  • condition, a statement that is evaluated to continue the loop

programacion-bucle-for-ampliado

Detailed FOR Loop Diagram

Which would be equivalent to the WHILE loop:

initialization;
while(condition) {
   // do something
   update;
}
Copied!

Examples of FOR Loops in Different Languages

Let’s see examples of FOR loop syntax in different languages.

In languages “inherited” from C, C++, C# or Java, the syntax of a FOR loop is as follows

for (int i = 0; i < 100; i++)
{
    // do something
}
Copied!

In JavaScript the syntax of the FOR loop is identical. Only the declaration of the variable i changes, which in this case is done with the reserved word let:

for (let i = 0; i < 100; i++)
{
    // do something
}
Copied!

On the other hand, Python does not have a FOR loop as such. It only has WHILE and FOR EACH, which acts on iterable collections. To emulate the result of a FOR loop, one of these structures must be used, for example like this:

for i in range(100):
    # do something
Copied!

In JavaScript the syntax of the FOR loop is identical. Only the declaration of the variable i changes, which in this case is done with the reserved word let:

for (let i = 0; i < 100; i++)
{
    // do something
}
Copied!

On the other hand, Python does not have a FOR loop as such. It only has WHILE and FOR EACH, which acts on iterable collections. To emulate the result of a FOR loop, one of these structures must be used, for example like this:

for i in range(100):
    # do something
Copied!

On the other hand, Python does not have a FOR loop as such. It only has WHILE and FOR EACH, which acts on iterable collections. To emulate the result of a FOR loop, one of these structures must be used, for example like this:

for i in range(100):
    # do something
Copied!

In these examples,

  • initialization, we create a temporary variable called i
  • update, each iteration, we increment i with i++
  • condition, we perform the loop while i < 100

Thus, we are performing the actions in the function body 100 times, which is the number of iterations it will take for i to stop being i < 100.

Careful, when counting from 0, we must stop at 99. That is, when i < 100. It’s common to make a mistake with this at the beginning.

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 omit it completely (and live perfectly without it).

Furthermore, 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 starting to program.

Best Practices Tips

One of the biggest problems with FOR loops is the incorrect use that has frequently been made of them. 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 make a WHILE loop:

for (;condition;)
{
}
Copied!

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)
{    
}
Copied!

Or in the condition:

for (initialization; veryComplexValidation(); i++)
{    
}
Copied!

Don’t do any of those things, they are messes 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);
}
Copied!

And if you need to evaluate a very complex condition, it’s likely that you should be using a WHILE loop.