G Gregg Walker Sep 11, 2007 #2 Vinnie -- ++i = pre-statement increment and i++ = post-statement increment When used as a statement by itself the effective difference is none. i.e. int i = 0; ++i; // i = 1 and int i = 0; i++; // i = 1 both result in i being incremented by 1 and the resulting value is 1. However when used in a more complex statement the position of the ++ can make a difference. The following statement results in the 2nd array value being assigned to x because i is incremented before the assignment to x takes place. int i = 0; int[] a = { 1, 2, 3 }; int x = a[++i]; // x = 2; i is incremented pre-statement (before execution of the statement). .... In this case however the 1st array value is assigned to x because i is incremented after the assignment to x takes place. int i = 0; int[] a = { 1, 2, 3 }; int x = a[i++]; // x = 1; i is incremented post-statement (after execution of the statement). HTH
Vinnie -- ++i = pre-statement increment and i++ = post-statement increment When used as a statement by itself the effective difference is none. i.e. int i = 0; ++i; // i = 1 and int i = 0; i++; // i = 1 both result in i being incremented by 1 and the resulting value is 1. However when used in a more complex statement the position of the ++ can make a difference. The following statement results in the 2nd array value being assigned to x because i is incremented before the assignment to x takes place. int i = 0; int[] a = { 1, 2, 3 }; int x = a[++i]; // x = 2; i is incremented pre-statement (before execution of the statement). .... In this case however the 1st array value is assigned to x because i is incremented after the assignment to x takes place. int i = 0; int[] a = { 1, 2, 3 }; int x = a[i++]; // x = 1; i is incremented post-statement (after execution of the statement). HTH
L Lee Sep 11, 2007 #3 what's the difference between " i++ " and " ++i " ? Thanks Click to expand... Assuming the appropriate declarations, then i = 1; x = i++; assigns 1 to x -- "++" is post-increment operator. i = 1; x = ++i; assigns 2 to x -- "++" is pre-increment operator.
what's the difference between " i++ " and " ++i " ? Thanks Click to expand... Assuming the appropriate declarations, then i = 1; x = i++; assigns 1 to x -- "++" is post-increment operator. i = 1; x = ++i; assigns 2 to x -- "++" is pre-increment operator.