Novo curso! Todo programador deveria aprender IA generativa!
Experimente uma aula grƔtis0
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 Respostas
+ 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 š
š