for循环中的++i和i++有区别吗?这仅仅是语法问题吗?
当前回答
一个(++i)是前增量,一个(i++)是后增量。区别在于表达式立即返回什么值。
// Psuedocode
int i = 0;
print i++; // Prints 0
print i; // Prints 1
int j = 0;
print ++j; // Prints 1
print j; // Prints 1
编辑:哎呀,完全忽略了循环方面的事情。在for循环中,当它是'step'部分(for(…;…。)),但它也可以在其他情况下发挥作用。
其他回答
问题是:
for循环中的++i和i++有区别吗?
答案是:不。
为什么每个答案都必须详细解释前增量和后增量,而这个问题甚至都没有问过?
这样的循环:
for (int i = 0; // Initialization
i < 5; // Condition
i++) // Increment
{
Output(i);
}
在不使用循环的情况下转换为下面的代码:
int i = 0; // Initialization
loopStart:
if (i < 5) // Condition
{
Output(i);
i++ or ++i; // Increment
goto loopStart;
}
现在你把i++还是++i作为增量有关系吗?不,它不会,因为增量操作的返回值是不重要的。i将在for循环体内的代码执行之后递增。
在这两种情况下,'i'将加1。
但是当你在表达式中使用它时,就有区别了,例如:
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
要理解FOR循环的作用
上图显示FOR可以转换为WHILE,因为它们最终具有完全相同的汇编代码(至少在gcc中)。所以我们可以把FOR分解成几部分,来理解它的功能。
for (i = 0; i < 5; ++i) {
DoSomethingA();
DoSomethingB();
}
等于WHILE版本
i = 0; //first argument (a statement) of for
while (i < 5 /*second argument (a condition) of for*/) {
DoSomethingA();
DoSomethingB();
++i; //third argument (another statement) of for
}
It means that you can use FOR as a simple version of WHILE: The first argument of FOR (int i) is executed, outside, before the loop. The third argument of FOR (i++ or ++i) is executed, inside, in the last line of the loop. TL:DR: no matter whether i++ or ++i, we know that when they are standalone, they make no difference but +1 on themselves. In school, they usually teach the i++ way, but there are also lots of people prefer the ++i way due to several reasons. NOTE: In the past, i++ has very little impact on the performance, as it does not only plus one by itself, but also keeps the original value in the register. But for now, it makes no difference as the compiler makes the plus one part the same.
关于++i和i++,不仅仅是循环和性能差异。++i返回一个l值,i++返回一个r值。基于此,可以对(++i)执行许多操作,但不能对(i++)执行许多操作。
1- It is illegal to take the address of post increment result. Compiler won't even allow you.
2- Only constant references to post increment can exist, i.e., of the form const T&.
3- You cannot apply another post increment or decrement to the result of i++, i.e., there is no such thing as I++++. This would be parsed as ( i ++ ) ++ which is illegal.
4- When overloading pre-/post-increment and decrement operators, programmers are encouraged to define post- increment/decrement operators like:
T& operator ++ ( )
{
// logical increment
return *this;
}
const T operator ++ ( int )
{
T temp( *this );
++*this;
return temp;
}
a++被称为后缀。
a加1,返回原来的值。
++a被称为前缀。
对a加1,返回新值。
C#:
string[] items = {"a","b","c","d"};
int i = 0;
foreach (string item in items)
{
Console.WriteLine(++i);
}
Console.WriteLine("");
i = 0;
foreach (string item in items)
{
Console.WriteLine(i++);
}
输出:
1
2
3
4
0
1
2
3
Foreach和while循环取决于您使用的增量类型。对于下面这样的for循环,它没有什么区别,因为你没有使用i的返回值:
for (int i = 0; i < 5; i++) { Console.Write(i);}
Console.WriteLine("");
for (int i = 0; i < 5; ++i) { Console.Write(i); }
0, 1, 2, 3, 4 0, 1, 2, 3, 4
如果使用所计算的值,则增量类型变得显著:
int n = 0;
for (int i = 0; n < 5; n = i++) { }