FOR loop in Javascript

Description and examples of the FOR loop.

We begin with this article to explore the control structures to produce loops or repetitions, beginning with the for loop, not because it is the simplest, but the most used in programming.

The FOR loop is used to repeat one or more instructions a certain number of times. Among all the loops, FOR is usually used when we know for sure the number of times we want it to be executed. The for loop syntax is shown below.

for (initialization; condition; update) { //statements to be executed in each iteration }

The FOR loop has three parts enclosed between the parentheses, which are used to define how we want the repetitions to be performed. The first part is initialization, which is executed only at the start of the first iteration of the loop. This part usually places the variable that we will use to keep track of the number of times the loop is executed.

The second part is the condition, which will be evaluated each time an iteration of the loop begins. Contains an expression to decide when to stop the loop, or rather, the condition that must be met for the loop to continue execution.

Finally we have the update, which is used to indicate the changes that we want to execute in the variables each time the iteration of the loop ends, before checking if it should continue executing.

After the for, the statements that we want to be executed in each iteration are placed, enclosed in braces.

See also  Sending, deleting and more in HTML forms

An example of using this loop can be seen below, where the numbers from 0 to 10 will be printed.

var i for (i=0;i<=10;i++) { document.write(i) document.write("
“) }

In this case, the variable ia 0 is initialized. As a condition to carry out an iteration, it must be met that the variable i is less than or equal to 10. As an update, the variable i will be increased by 1.

As you can see, this loop is very powerful, since in a single line we can indicate many different and very varied things, which allows a quick configuration of the loop and enormous versatility.

For example, if we want to write the numbers from 1 to 1,000 two by two, the following loop will be written.

for (i=1;i<=1000;i+=2) document.write(i)

If we look closely, in each iteration we update the value of i by increasing it by 2 units.

Note: Another detail, we do not use the braces encompassing the FOR loop instructions because it only has one statement and in this case it is not forced, as it happened with the IF instructions.

If we want to count down from 343 to 10 we would use this loop.

for (i=343;i>=10;i–) document.write(i)

In this case we decrease the variable i by one unit in each iteration, starting at the value 343 and whenever the variable has a value greater than or equal to 10.

for loop example exercise

We are going to take a break to assimilate the for loop with an exercise that does not contain any difficulty if we have understood how the loop works.

See also  Usability on the web

It is about making a loop that writes in a web page the headings from

to

with a text that says “Heading of level x”.

What we want to write on a web page using Javascript is the following:

Level 1 Header

Level 2 Header

Level 3 Header

Level 4 Header

Level Header 5
Level 6 Header

To do this we have to make a loop that starts at 1 and ends at 6 and in each iteration we will write the header it touches.

for (i=1;i<=6;i++) { document.write("Level Header ” + i + ““) }

This script can be .

Now that we’re familiar with the for loop, we’re ready to learn how to handle other control structures for iterating, such as .

Loading Facebook Comments ...
Loading Disqus Comments ...