Java question

duragezic

Lifer
Oct 11, 1999
11,234
4
81
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?
 

MrChad

Lifer
Aug 22, 2001
13,507
3
81
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
 

SleepWalkerX

Platinum Member
Jun 29, 2004
2,649
0
0
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.
 

Templeton

Senior member
Oct 9, 1999
467
0
0
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

 

kamper

Diamond Member
Mar 18, 2003
5,513
0
0
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.
 

MrChad

Lifer
Aug 22, 2001
13,507
3
81
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

:eek:

I stand corrected.
 

Markbnj

Elite Member <br>Moderator Emeritus
Moderator
Sep 16, 2005
15,682
14
81
www.markbetz.net
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.
 

kamper

Diamond Member
Mar 18, 2003
5,513
0
0
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.
 

Markbnj

Elite Member <br>Moderator Emeritus
Moderator
Sep 16, 2005
15,682
14
81
www.markbetz.net
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.
 

kamper

Diamond Member
Mar 18, 2003
5,513
0
0
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 :p