Loop | Sololearn: Learn to code for FREE!
Novo curso! Todo programador deveria aprender IA generativa!
Experimente uma aula grƔtis
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 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.
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