Can someone explain to me? JS Increment Decrement | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 8

Can someone explain to me? JS Increment Decrement

Code 1: <script> var x = 1; for(;x<6;x+=2){ x=x*x; } document.write(x) </script> here in this code, even after the condition is false, the part that increments is executed last time. --###---####----#####-----#####------###### Code 2: <script> var x = 0; for(;x<8;x++){} document.write(x); for(;x>4;x-=2){} document.write(x); </script> here in this code, even after the condition is false, the incrementing part is no longer executed. Why does it happen? or am I wrong?

19th Oct 2017, 4:09 PM
Malkon F
Malkon F - avatar
2 Answers
+ 10
thanks, but the problem was not about that. I was able to find a solution. I was confusing the order in which the parts are evaluated in the for, the correct form would be: 1. Initializes the condition. 2.Check the condition. 3. Execute what you have in your body. 4.increment the variable. Doing this way the variable x of the first code gets value 11.;) I was doing it wrong way: 1. Initializes the condition. 2.Check the condition. 3. increment the variable 4.Execute what you have in your body.
19th Oct 2017, 8:36 PM
Malkon F
Malkon F - avatar
+ 1
Hey buddy , I guess it's related to the process of adding here's example x++ //which means increase after this step execution not now ++x //which means add in this moment don't wait you can test it more by for example var a = 2; var b = a++; //b = 2 //a = 3 so what happens in Code 1 that it increment at the same time so the result of i already increased , but in the second not .
19th Oct 2017, 8:04 PM
amr hassan (3Mo0or)
amr hassan (3Mo0or) - avatar