0
Loop
Hello i havĂ© several questions on loop: -There are several ways to write loops, which is the best ? -In the loop 1 what can I put in place of i++, they say we can put iâor something else, but it doesnât give a result... -In the loop 4 what i can put in text or in the place of text and what is it used for ? https://code.sololearn.com/WzgZkRgEvE5a/?ref=app https://code.sololearn.com/WvRBpyYtYfML/?ref=app https://code.sololearn.com/Wf3GvteUPheb/?ref=app https://code.sololearn.com/WdtvMiwB47Bf/?ref=app Thankâs
6 Answers
+ 6
The For Loop
Itâs the most basic of loops in JavaScript and is quite versatile.
for (i = 0; i < 10; i++) {
// do something
}
Our for loop consists of three statements, one that is executed before our loop starts ( i = 0 ), one that defines how long our loop should run ( i < 10 ), and one that is executed after each loop ( i++ ).
In this example, we are setting i = 0 before our loop starts. We will continue to loop as long as i < 10, and each iteration of the loop will increase i by one. Finally, within our brackets is the code that will be run on each iteration of the loop.
+ 5
Skipping parts! ;)
Any part of 'for' can be skipped.
For example, we can omit 'begin' if we donât need to do anything at the loop start.
let i = 0; // we have i already declared and assigned
for ( ; i < 3; i++) { // no need for "begin"
alert( i ); // 0, 1, 2
}
We can also remove the 'step' part:
let i = 0;
for ( ; i < 3; ) {
alert( i++ );
}
The loop became identical to while (i < 3).
We can actually remove everything, thus creating an infinite loop:
for ( ; ; ) {
// repeats without limits
}
Please note that the two 'for' semicolons ; must be present, otherwise it would be a syntax error.
+ 5
Axelle
In fact, they are all the same!đ
You can use 'for' loop in a different ways or combine them!
+ 4
Axelle đđ
You are welcome! đ
I'm glad it helped! đ
+ 1
ok thanks
so i haven't need of the other version ..?!
+ 1
thank you very much
itâs much clearer with you đ
đ