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

14th Dec 2018, 10:22 PM
Axelle
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.
14th Dec 2018, 10:49 PM
Danijel Ivanović
Danijel Ivanović - avatar
+ 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.
14th Dec 2018, 11:03 PM
Danijel Ivanović
Danijel Ivanović - avatar
+ 5
Axelle In fact, they are all the same!👍 You can use 'for' loop in a different ways or combine them!
15th Dec 2018, 7:38 PM
Danijel Ivanović
Danijel Ivanović - avatar
+ 4
Axelle 👍😉 You are welcome! 😊 I'm glad it helped! 😆
15th Dec 2018, 8:17 PM
Danijel Ivanović
Danijel Ivanović - avatar
+ 1
ok thanks so i haven't need of the other version ..?!
15th Dec 2018, 6:47 PM
Axelle
+ 1
thank you very much it’s much clearer with you 😅😇
15th Dec 2018, 7:42 PM
Axelle