for循环中的++i和i++有区别吗?这仅仅是语法问题吗?
当前回答
对于循环可能会有不同。这是后增量/前增量的实际应用。
int i = 0;
while(i++ <= 10) {
Console.Write(i);
}
Console.Write(System.Environment.NewLine);
i = 0;
while(++i <= 10) {
Console.Write(i);
}
Console.ReadLine();
虽然第一个数到11,循环11次,但第二个不是。
这通常用于简单的while(x——> 0);- -循环迭代数组中的所有元素(这里不包括foreach-construct)。
其他回答
Pre-increment ++i增加i的值,并计算为新的增量值。
int i = 3;
int preIncrementResult = ++i;
Assert( preIncrementResult == 4 );
Assert( i == 4 );
后增量i++增加i的值,并计算为原始的非增量值。
int i = 3;
int postIncrementResult = i++;
Assert( postIncrementtResult == 3 );
Assert( i == 4 );
在c++中,如果可以使用前递增,则通常是首选。
这是因为如果使用后增量,它可能要求编译器必须生成创建额外临时变量的代码。这是因为被递增的变量的前值和新值都需要保存在某个地方,因为它们可能在被求值的表达式的其他地方被需要。
因此,至少在c++中,可以有性能差异来指导您选择使用哪一种。
这主要只在被递增的变量是用户定义的具有重写++操作符的类型时才会出现问题。对于基本类型(int等),没有性能差异。但是,作为指导原则,坚持使用前增量操作符是值得的,除非确实需要后增量操作符。
这里还有一些讨论。
In C++ if you're using STL, then you may be using for loops with iterators. These mainly have overridden ++ operators, so sticking to pre-increment is a good idea. Compilers get smarter all the time though, and newer ones may be able to perform optimizations that mean there's no performance difference - especially if the type being incremented is defined inline in header file (as STL implementations often are) so that the compiler can see how the method is implemented and can then know what optimizations are safe to perform. Even so, it's probably still worth sticking to pre-increment because loops get executed lots of times and this means a small performance penalty could soon get amplified.
在其他语言(如c#)中,c++操作符不能重载,在性能上没有区别。在循环中用于使循环变量前进,前增量操作符和后增量操作符是等效的。
更正:在c#中重载c++是允许的。不过,与c++相比,在c#中,你不能独立地重载前版本和后版本。因此,我假设在c#中调用++的结果没有被赋值给变量或用作复杂表达式的一部分,那么编译器会将++的前后版本简化为执行等效的代码。
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++) { }
在这两种情况下,'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
在某些情况下,++i和i+1可能会给出不同的结果,-i, i - 1等也是如此。
这并不是因为递增和递减操作符的工作方式有缺陷,而是因为新程序员有时会忽略一个小事实。
根据经验,不要在数组的方括号内使用inc/dec。例如,我不会用arr[++ I]来代替arr[I + 1]。虽然两者得到的i值是一样的,但这里我们忽略了一些东西。
如果循环条件基于i的执行值,那么将arr[i + 1]替换为arr[++i]将导致错误。为什么?
假设i = 5,那么arr[i + 1]意味着arr[6],而arr[++i]虽然意味着arr[6],但也会将i的值改变为6,这可能不是我们想要做的事情。我们可能不想改变i的值,但由于一个简单的++/——操作符,我们改变了这个值。
所以在使用++/——操作符时要小心。
我希望我能使我的观点更容易理解。
问题是:
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循环体内的代码执行之后递增。
推荐文章
- 如何在for循环中注释类型?
- 在支持循环和函数的语言中,是否存在“goto”的合法用例?
- 为什么不使用异常作为常规的控制流呢?
- 列表推导式和函数式函数比for循环更快吗?
- 什么是序列化?
- 我怎么能循环通过一个列表<T>和抓取每个项目?
- 每个递归都可以转换成迭代吗?
- 去下一次迭代在java For循环
- 什么是ORM,它是如何工作的,我应该如何使用它?
- 我能在服务器端应用程序(PHP、Ruby、Python等)上读取URL的哈希部分吗?
- 元素在“for(…in…)”循环中排序
- 多少个参数是太多?
- 是否有可能在没有迭代器变量的情况下实现Python for range循环?
- 对于不可变集合上的非突变“add”方法,最好的名称是什么?
- foo到底是什么意思?