i++ will increment the value of i, but return the original value that i held before being incremented.
i = 1;j = i++;(i is 2, j is 1)
For a for loop, either works. ++i seems more common, perhaps because that is what is used in K&R.
In any case, follow the guideline "prefer ++i over i++" and you won't go wrong.
There's a couple of comments regarding the efficiency of ++i and i++. In any non-student-project compiler, there will be no performance difference. You can verify this by looking at the generated code, which will be identical.
As @OnFreund notes, it's different for a C++ object, since operator++() is a function and the compiler can't know to optimize away the creation of a temporary object to hold the intermediate value.
int i, j, k, l;i = 1; //initialize int i with 1j = i+1; //add 1 with i and set that as the value of j. i is still 1k = i++; //k gets the current value of i, after that i is incremented. So here i is 2, but k is 1l = ++i; // i is incremented first and then returned. So the value of i is 3 and so does l.cout << i << ' ' << j << ' ' << k << ' '<< l << endl;return 0;
int j = 0;System.out.println(j); // 0System.out.println(j++); // 0. post-increment. It means after this line executes j increments.
int k = 0;System.out.println(k); // 0System.out.println(++k); // 1. pre increment. It means it increments first and then the line executes
当它带有OR和AND操作符时,它变得更有趣。
int m = 0;if((m == 0 || m++ == 0) && (m++ == 1)) { // False// In the OR condition, if the first line is already true// then the compiler doesn't check the rest. It is a// technique of compiler optimizationSystem.out.println("post-increment " + m);}
int n = 0;if((n == 0 || n++ == 0) && (++n == 1)) { // TrueSystem.out.println("pre-increment " + n); // 1}
在数组中
System.out.println("In Array");int[] a = { 55, 11, 15, 20, 25 };int ii, jj, kk = 1, mm;ii = ++a[1]; // ii = 12. a[1] = a[1] + 1System.out.println(a[1]); // 12
jj = a[1]++; // 12System.out.println(a[1]); // a[1] = 13
mm = a[1]; // 13System.out.printf("\n%d %d %d\n", ii, jj, mm); // 12, 12, 13
for (int val: a) {System.out.print(" " + val); // 55, 13, 15, 20, 25}
在c++中,指针变量的后/前增量
#include <iostream>using namespace std;
int main() {
int x = 10;int* p = &x;
std::cout << "address = " << p <<"\n"; // Prints the address of xstd::cout << "address = " << p <<"\n"; // Prints (the address of x) + sizeof(int)std::cout << "address = " << &x <<"\n"; // Prints the address of x
std::cout << "address = " << ++&x << "\n"; // Error. The reference can't reassign, because it is fixed (immutable).}
int i = 10, j = 10;
printf ("i is %i \n", i);printf ("i++ is %i \n", i++);printf ("i is %i \n\n", i);
printf ("j is %i \n", j);printf ("++j is %i \n", ++j);printf ("j is %i \n", j);
结果是:
//Remember that the values are i = 10, and j = 10
i is 10i++ is 10 //Assigns (print out), then incrementsi is 11
j is 10++j is 11 //Increments, then assigns (print out)j is 11
注意之前和之后的情况。
for循环
至于在for循环的增量块中应该使用它们中的哪一个,我认为我们能做的最好的决定是使用一个很好的例子:
int i, j;
for (i = 0; i <= 3; i++)printf (" > iteration #%i", i);
printf ("\n");
for (j = 0; j <= 3; ++j)printf (" > iteration #%i", j);
#include<stdio.h>
int main(int argc, char* argv[]){unsigned int i=0, a;printf("i initial value: %d; ", i);a = i++;printf("value returned by i++: %d, i after: %d\n", a, i);i=0;printf("i initial value: %d; ", i);a = ++i;printf(" value returned by ++i: %d, i after: %d\n",a, i);}
输出结果为:
i initial value: 0; value returned by i++: 0, i after: 1i initial value: 0; value returned by ++i: 1, i after: 1