+ 3

Confusion, x++ is adding 1 when we apply this in loop,why?

when we apply in loop, we write post increment x++ and postincrement add 1 after each cycle. for(i=0; i<10; i++) { System.out.println("Why this i++ add 1"); }

22nd Oct 2016, 6:33 PM
Hamza Zafar Ahmed
Hamza Zafar Ahmed - avatar
4 Answers
+ 8
i++ or ++i are equivalent (to i = i+1) when used as a standalone statement, which is the case here. Post-incrementation doesn't mean that the incrementation isn't done. Maybe your confusion stems from a wrong understanding of how a for loop works: for(int i=0; i<10; i++) { System.out.println(i); } is equivalent to: int = 0; while (i < 10) { System.out.println(i); i++; } Edit: Woops, thanks for the correction, Bobby.
22nd Oct 2016, 7:05 PM
Zen
Zen - avatar
+ 2
Just so you are aware, Zen's information is correct however his syntax is for a different language. instead of cout<<i<<endl, use System.out.println(i); edit: nevermind, he fixed it. :)
25th Oct 2016, 8:12 PM
Bobby Turnip (Scye)
Bobby Turnip (Scye) - avatar
0
The difference in priority
22nd Oct 2016, 10:07 PM
Oleg
Oleg - avatar
0
it is because the program contains counting backwards
27th Nov 2016, 10:55 AM
sachin pagariya