Difference between i++ and ++i in a loop?

2018-12-31 04:08发布

Is there a difference in ++i and i++ in a for loop? Is it simply a syntax thing?

21条回答
不再属于我。
2楼-- · 2018-12-31 04:15

In C# there is no difference when used in a for loop.

for (int i = 0; i < 10; i++) { Console.WriteLine(i); }

outputs the same thing as

for (int i = 0; i < 10; ++i) { Console.WriteLine(i); }

As others have pointed out, when used in general i++ and ++i have a subtle yet significant difference:

int i = 0;
Console.WriteLine(i++);   // Prints 0
int j = 0;
Console.WriteLine(++j);   // Prints 1

i++ reads the value of i then increments it.

++i increments the value of i then reads it.

查看更多
公子世无双
3楼-- · 2018-12-31 04:16

As @Jon B says, there is no difference in a for loop.

But in a while or do...while loop, you could find some differences if you are making a comparison with the ++i or i++

while(i++ < 10) { ... } //compare then increment

while(++i < 10) { ... } //increment then compare
查看更多
人间绝色
4楼-- · 2018-12-31 04:16

Yes, there is a difference between ++i and i++ in a for loop, though in unusual use cases; when a loop variable with increment/decrement operator is used in the for block or within the loop test expression, or with one of the loop variables. No it is not simply a syntax thing.

As i in a code means evaluate the expression i and the operator does not mean an evaluation but just an operation;

  • ++i means increment value of i by 1 and later evaluate i,
  • i++ means evaluate i and later increment value of i by 1.

So, what are obtained from each two expressions differ because what is evaluated differs in each. All same for --i and i--

For example;

let i = 0

i++ // evaluates to value of i, means evaluates to 0, later increments i by 1, i is now 1
0
i
1
++i // increments i by 1, i is now 2, later evaluates to value of i, means evaluates to 2
2
i
2

In unusual use cases, however next example sounds useful or not does not matter, it shows a difference

for(i=0, j=i; i<10; j=++i){
    console.log(j, i)
}

for(i=0, j=i; i<10; j=i++){
    console.log(j, i)
}
查看更多
与君花间醉酒
5楼-- · 2018-12-31 04:17

Yes, there is. The difference is in the return value. The return value of "++i" will be the value after incrementing i. The return of "i++" will be the value before incrementing. This means that code that looks like the following:

int a = 0;
int b = ++a; // a is incremented and the result after incrementing is saved to b.
int c = a++; // a is incremented again and the result before incremening is saved to c.

Therefore, a would be 2, and b and c would each be 1.

I could rewrite the code like this:

int a = 0; 

// ++a;
a = a + 1; // incrementing first.
b = a; // setting second. 

// a++;
c = a; // setting first. 
a = a + 1; // incrementing second. 
查看更多
只靠听说
6楼-- · 2018-12-31 04:17

There is no actual difference in both cases 'i' will be incremented by 1.

But there is a difference when you use it in an expression, for example:

int i = 1;
int a = ++i;
// i is incremented by one and then assigned to a.
// Both i and a are now 2.
int b = i++;
// i is assigned to b and then incremented by one.
// b is now 2, and i is now 3
查看更多
查无此人
7楼-- · 2018-12-31 04:18

One (++i) is preincrement, one (i++) is postincrement. The difference is in what value is immediately returned from the expression.

// Psuedocode
int i = 0;
print i++; // Prints 0
print i; // Prints 1
int j = 0;
print ++j; // Prints 1
print j; // Prints 1

Edit: Woops, entirely ignored the loop side of things. There's no actual difference in for loops when it's the 'step' portion (for(...; ...; )), but it can come into play in other cases.

查看更多
登录 后发表回答