+ 1

Increments in for loop

for(int x = 0; x <10; x++){System.out.print(x);} Shouldn't this increment x by 1 before it reaches print(x);} ? i cant see any diffrence as ++x x++ both outputting 0123456789? also im assuming becuase 10<10 is false this terminates the loop.?

16th Aug 2017, 9:55 AM
D_Stark
D_Stark - avatar
2 Réponses
0
I only use x++ in the for parameter. elsewhere, I use x+=1;
16th Aug 2017, 10:32 AM
Gao Xiangshuai
Gao Xiangshuai - avatar
0
This is what you should know. Whether you use x++ or ++x, you will get the same result. Take note of this important point though, if for instance x=1; ++x will give you 2 when you print it out where as x++ will give you 1. 1 because thats a post increment meaning telling the pc that okay, I need you to add 1 to my current value after displaying it.
1st Sep 2017, 11:22 AM
Amos Aidoo
Amos Aidoo - avatar