Logo of Computer Classes

Repetitive Structures or Loops FROM or FOR

These structures are instructions that are repeated in a loop (something that is repeated over and over again).

The variable that "keeps" the count of the times the loop has been executed has been called the counter variable.

FOR structures have the peculiarity that the counter variable is inside the loop and it is not necessary to assign the value to it (or define it) outside the loop, and also, when the program reaches the loop, the instructions inside the loop will always be carried out, a number of times that we set.

There are several, but this one that we explain is the most used.

Let's suppose that we are thinking of a program that must REPEAT an action some times.

A more concrete example. The computer has misbehaved, and as punishment, we are going to make it print the phrase "I promise to be good" on the screen 3000 times.

How do we do it? Did we write the relevant instruction 3000 times?

Punishment is supposed to be for the machine, not oneself!

Let's see what the pseudocode would look like:


Start Loop From

from i = 1 to i <= 3,000

i = i + 1;

Write 'I promise to be good';

end from;


As we can see, the variable i (called the counter) is not defined before the loop. when entering the loop i it will be worth 1 (it takes the initial value that we put in the first equal, in our case i = 1.

Then we tell it until the value of i the loop will be repeated, in our case until i is worth less than or equal to 3000.

Later we put how much the variable increases, in our case we add 1 to the value of variable i; i = i + 1.

At the end we put the orders that we want the program to do every time it loops; in our case write on the screen "I promise to be good".

As you can see, the first time the program enters the loop i is worth 1, then i is worth 2 (1 is added to it) and then it writes the phase.

Before exiting the loop, it reevaluates the condition to see if it continues to meet it, if so, redo the entire loop.

Is that so?

Of course, because i = 2 is still less than 3000.

But be careful the second time you do the loop, i will take the value of 3, since it will add 1 to the value it had, and remember that since you already did the loop once, now i = i + 1 will be 3; since i = 2 +1.

You see that every time you do the loop once the value of i increases 1. This is what is called "the step".

We could do the loop with step 2 just by doing i = i +2.

Well continuing with the loop, it turns out that this loop will repeat itself until i is equal to or less than 3000.

Well, in all these repetitions the computer will write the phrase: I promise to be good. Punishment accomplished.

In real programming language, For structures are formed:

for (i = 1; i <= 3000; i = i + 1)

Sometimes you will see this:

for (i = 1; i <= 3000; i ++)

i ++ means the same as i = i +1; is to specify step 1 of i but in another way, nothing else

Remember in real programming the word for is used, and not from. And normally the variable in the for loops is called i.

What if we wanted to put a decreasing step?

In other words, the value of i was decreasing each time the loop is repeated.

Well, very simple putting i = i - 1.

Exercise: Program that writes the numbers from 1 to 10 with FOR

Date update on 2021-03-31. Date published on 2021-03-31. Category: Computer class Author: Oscar olg Fuente: areatecnologia