About increment in for loop | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

About increment in for loop

why always in the inctemebt of the for loop we use the postfix x++ and not prefix ?..although they have the same result

14th Jun 2017, 5:08 PM
De Vinci
2 Answers
+ 1
It doesn't make any difference, both of them are correct. The difference in prefix and postfix is important when you assign values (i.e., y=x++ and y=++x are different things), but it makes no difference if what you execute is just a bare "x++" or "++x", as in the for loop case. I think that "x++" in for loops is just more "traditional", but I've seen people using "++x" as well.
14th Jun 2017, 5:33 PM
Matteo Seclì
Matteo Seclì - avatar
+ 1
In theory, prefix is actually a (very) bit faster, so you should rather focus on that rather than the tradition. But it the performance gained is not significant so only Pros care about it. :D
14th Jun 2017, 5:48 PM
Norbivar
Norbivar - avatar