I don't know why I find it very hard to understand how the for...loop works. | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 13

I don't know why I find it very hard to understand how the for...loop works.

For instance I need an explanation on why the output of the code below is 25 var x = 0; for (i=0; i < 5; ++i) { x += 2; x += 2; } x += i console.log(x)

21st May 2019, 7:32 AM
eMBee
eMBee - avatar
16 Answers
+ 13
x and i starts off as 0. in the each time the loop adds 4 to x. The loop runs 5 times: when it is 0, 1, 2, 3, 4. x will be 20, because 4*5 = 20. The last part runs at the end of every loop, so when the loop executed for the 5th time, i was 4. But then it added one, so it became 5, and the condition wasn't true anymore so it stopped. 20 + 5 = 25, so the output is 25
21st May 2019, 7:47 AM
Airree
Airree - avatar
+ 16
Mofey var x = 0; for (i=0; i<5; ++i) { x + = 2; x + = 2; } x += i console.log(x) When i = 0 then x = 4 //workings for (i=0; i<5; ++i) { x = 0 + 2; //firstly x = 2 //then x = 2 + 2; // later x = 4 } //now ( i = 0 and x = 4) and the loop will run again since "i" isn't up to 5 When i = 1 then x = 8 //workings for (i=1; i<5; ++i) { x = 4 + 2; //firstly x = 6 //then x = 6 + 2; // later x = 8 } //now ( i = 1 and x = 8) and the loop will run again since "i" isn't up to 5 When i = 2 then x = 12 //workings for (i=2; i<5; ++i) { x = 8 + 2; //firstly x = 10 //then x = 10 + 2; // later x = 12 } //now ( i = 2 and x = 12) and the loop will run again since "i" isn't up to 5 When i = 3 then x = 16 //workings for (i=4; i<5; ++i) { x = 12 + 2; //firstly x = 14 //then x = 14 + 2; // later x = 16 } //now ( i = 3 and x = 16) and the loop will run again since "i" isn't up to 5 Continue.......
21st May 2019, 10:31 AM
Lawz
Lawz - avatar
+ 12
When i = 4 then x = 20 //workings for (i=4; i<5; ++i) { x = 16 + 2; //firstly x = 18 //then x = 18 + 2; // later x = 20 } //now ( i = 4 and x = 20) ++i will increase "i" to 5 now and then check (i<5;) then the loop will stop... So finally x = 20 i = 5.. Now x += i equals x = 20 + 5. console.log(x) //25
21st May 2019, 10:37 AM
Lawz
Lawz - avatar
+ 8
Mofey notice me when you find this book
21st May 2019, 8:28 AM
Hafsa Mohamed
Hafsa Mohamed - avatar
+ 7
Understand one thing that i have confused for a while understand that the condition inside the for loop is checked just before the body of the for loop is executed var x = 0; for (i=0; i < 5; ++i) { x += 2; x += 2; } x += i console.log(x) The last iteration of i becomes 5 but the body of the for loop will not be executed because the condition i<5 becomes false and remember that i has a glopal scope and can be accessed outside the loop so i outside the becomes 5 You can try console.log(i) //5
21st May 2019, 8:25 AM
Hafsa Mohamed
Hafsa Mohamed - avatar
+ 6
Airree i agree with your explanation
21st May 2019, 8:14 AM
Hafsa Mohamed
Hafsa Mohamed - avatar
+ 6
Airree even though you explained it in the simplest possible form, it still took me the best of my understanding to assimilate that explanation. Thanks
21st May 2019, 8:24 AM
eMBee
eMBee - avatar
+ 6
I need someone to please recommend a book that could further broaden my horizon on for...loop
21st May 2019, 8:25 AM
eMBee
eMBee - avatar
+ 5
Why insted of x +=2; x +=2; don’t do just one line of x +=4; ???? i have hard time to uderstand it var x = 0; for (i=0; i < 5; ++i) { x += 4; } x += i console.log(x)
21st May 2019, 2:19 PM
Robert Rydlewski
Robert Rydlewski - avatar
21st May 2019, 9:39 AM
Sonic
Sonic - avatar
+ 2
for (i = 3; i<30; i++){...} Can be thought as: var i = 3; (loop until break){ if (!(i < 30)){ break; } ... i++; }
21st May 2019, 7:57 AM
Seb TheS
Seb TheS - avatar
+ 2
Here you have the instruction for(...) How you can see the variable for iterate i is 0 the first time i=0, and it will be incrementen by 1 (++i), while i <5, it means that i can take five values i = 0, 1, 2, 3 and 4, it means that the instructions corresponding yo the for(...) Will be repeated 4 times, as you can see x is incremented by 4, first time by 2 (x+=2) and second time (x+=2) also by 2, then you hace 4 repeated 5 times, it is 20, and next you add i (x += i), that hace the value of 5, and it is 25
23rd May 2019, 11:07 AM
Braddy Iván Jimenez Morales
Braddy Iván Jimenez Morales - avatar
+ 1
The easiest way, is to create a table on the papper, in which you will write down the values, after every iteration. More specific, you will put the values of i , x, x', x result on the orizontal axe and the iteration values : 1,2,3,4,5 on the vertical axe. Your table can be a 4×5 , 4 cols ×5 rows. Good luck!
21st May 2019, 3:10 PM
MarieK
MarieK - avatar
+ 1
Thanks for respond ;) however I still dont get it. in your example 4 cols x 5 rows equal 20.. but answer is 25.. where the 5 coming from ?? ;(
22nd May 2019, 2:14 PM
Robert Rydlewski
Robert Rydlewski - avatar
+ 1
Graphic Table ___________|_____x _____x'_____x"_____ x.result__ Iteration 1 | Iteration 2 | Iteration 4 | Iteration 5 |
22nd May 2019, 2:30 PM
MarieK
MarieK - avatar
+ 1
Thank You so much !!!!! you’re the Man :) I finally understood it, with make my day ;) enjoy rest of your day buddy ;) and happy coding. Again thanks 🙏 a lot 😉🙌
23rd May 2019, 11:16 AM
Robert Rydlewski
Robert Rydlewski - avatar