Language: EN


The FOR Loop

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 used and powerful control structures.

We have already seen the WHILE and DO-WHILE loops, which are the most basic loops.

As the loops were used, it happened very frequently 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 in a simple way. For example, if you wanted to do an action 100 times, you could use a counter and do:

counter = 0
while(counter < 100)
    // do something
    counter += 1;

But this structure was repeated so much, that they said “hey, what if we do something to make it easier?“. And that’s how the FOR loop arises.

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

	// do something

This diagram would look like this:


Evolution of the FOR loops

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

Here the loop was defined as:

for i := 1 step 1 until 10 do

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, reaching the most extended syntax, which we find today in many languages such as C:

for(initialization; condition; update)
	// do something

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, statement that is executed in each iteration of the loop
  • condition, statement that is evaluated to continue the loop


Which would be equivalent to the WHILE loop:

while(condition) {
   // do something

Examples of FOR loops in different languages

We are going to see examples of FOR loop syntax in different languages.

In “heir” languages like 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. Only the declaration of the variable i changes, which in this case is done with the keyword let:

for (let i = 0; i < 100; i++)
    // do something

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

for i in range(100):
    # do something

In this example,

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

With this, we are performing the actions of the function body 100 times, which is the number of iterations that i will take to no longer be i < 100.

Be careful, when counting from 0, we must stop at 99. In other words, when i < 100. It is common at first to make mistakes with this.

The FOR loop is one of the most popular and used in programming. To a large extent, because of the significant influence that C++ has had on the world of programming.

However, for practical purposes, it is not as useful and powerful as other loops such as WHILE or FOREACH. In fact, we have seen that languages like Python completely omit it (and live perfectly fine without it).

Furthermore, although it arises 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 beginners in programming.

Best practices and cleaning tips Tips

One of the biggest problems with FOR loops is the incorrect use that is frequently made of them. Having three statements, which are also optional, gives a lot of freedom. But it also allows “very original” but not very clean practices.

For example, at times, leaving only the condition to make a WHILE loop:

for (;condition;)

And even omitting the three statements to achieve an infinite loop, from which you can only exit with a break.

Don’t do that, use each loop for what it is.

Another common defect 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. Keep the three statements as simple as possible.

In general, the usual structure is the only one you should use. If you need to do any operation in 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 should probably be using a WHILE loop.