• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

Java question

What is the difference between these two statements?

I know that i++ increments by one whatever i is, but I don't understand what ++i means. Because I'm looking at loops that I thought simply went from 0 to some number "for ( int i=0; i<100; i++ )", but they are written as: "for ( int i=0; i<100; ++i )". So they are not the same operation?
 
Nope.

for (int i=0; i < 100; i++){
....System.out.println(i + " ");
}

will output: 0 1 2 3 4 5 6 ... 99

for (int i=0; i < 100; ++i){
....System.out.println(i + " ");
}

will output: 1 2 3 4 5 6 7 ... 100
 
If you have the ++ before the variable then it'll add one to the variable first (in this for loop it'll add one before it starts). If you have the ++ after then it'll add one afterwards.
 
Originally posted by: MrChad
Nope.

for (int i=0; i < 100; i++){
....System.out.println(i + " ");
}

will output: 0 1 2 3 4 5 6 ... 99

for (int i=0; i < 100; ++i){
....System.out.println(i + " ");
}

will output: 1 2 3 4 5 6 7 ... 100


No, they will output the same thing. the loop itirator isn't evaluated until after the first pass through the loop.

The difference is in expressions:

int i = 5;
int j = ++i;
//i is now 6, j is 6

int i = 5;
int j = i++;
//i is now 6, j is 5 since i incremented after assignment

 
Originally posted by: Templeton
No, they will output the same thing. the loop itirator isn't evaluated until after the first pass through the loop.
Confirmed.
The difference is in expressions:

int i = 5;
int j = ++i;
//i is now 6, j is 6

int i = 5;
int j = i++;
//i is now 6, j is 5 since i incremented after assignment
Good example. In more depth: both fragments have both an action (increment) and a value. With pre-increment, the action takes place before the evaluation. With post-increment, the evaluation takes place before the action.
 
Originally posted by: Templeton
Originally posted by: MrChad
Nope.

for (int i=0; i < 100; i++){
....System.out.println(i + " ");
}

will output: 0 1 2 3 4 5 6 ... 99

for (int i=0; i < 100; ++i){
....System.out.println(i + " ");
}

will output: 1 2 3 4 5 6 7 ... 100


No, they will output the same thing. the loop itirator isn't evaluated until after the first pass through the loop.

The difference is in expressions:

int i = 5;
int j = ++i;
//i is now 6, j is 6

int i = 5;
int j = i++;
//i is now 6, j is 5 since i incremented after assignment

😱

I stand corrected.
 
Templeton is correct. The difference is that the prefix operator (++i) applies the operator before the value is "taken" for use in the operation. The postfix operator (i++) applies the operator after the value is taken. Can lead to some subtle bugs, but in practice postfix is the right way to go 90% of the time.
 
Originally posted by: Markbnj
Can lead to some subtle bugs, but in practice postfix is the right way to go 90% of the time.
IMHO, in practice, the right way to go is to never use either in combination with another statement. The extra line of code is well worth the clarity.
 
IMHO, in practice, the right way to go is to never use either in combination with another statement. The extra line of code is well worth the clarity.

:thumbsup: Agree 100%. There are relatively few good uses for either form.
 
The only place I've really seen them inlined into other code is from my math-oriented comp-sci profs who still write in fortran for fun and seem to think that extremely densely packed code with a multitude of single-lettered variable names is somehow more elegant than code that humans can read 😛
 
Back
Top