博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
循环前增加和后增加之间的区别?
阅读量:2291 次
发布时间:2019-05-09

本文共 3298 字,大约阅读时间需要 10 分钟。

本文翻译自:

Is there a difference in ++i and i++ in a for loop? for循环中的++ii++有区别吗? Is it simply a syntax thing? 这仅仅是语法问题吗?


#1楼

参考:


#2楼

They both increment the number. 它们都增加数字。 ++i is equivalent to i = i + 1 . ++i等于i = i + 1

i++ and ++i are very similar but not exactly the same. i++++i非常相似,但不完全相同。 Both increment the number, but ++i increments the number before the current expression is evaluated, whereas i++ increments the number after the expression is evaluated. 两者都增加数字,但是++i增加对当前表达式求值之前的数字,而i++增加对表达式求值之后的数字。

int i = 3;int a = i++; // a = 3, i = 4int b = ++a; // b = 4, a =

Check . 检查 。


#3楼

There is no difference if you are not using the value after increment in the loop. 如果在循环中不使用增量后的值,则没有区别。

for (int i = 0; i < 4; ++i){cout<

Both the loops will print 0123. 这两个循环都将打印0123。

But the difference comes when you uses the value after increment/decrement in your loop as below: 但是,当您在循环中使用递增/递减后的值时,会出现如下差异:

Pre Increment Loop: 预增量循环:

for (int i = 0,k=0; i < 4; k=++i){cout<<<" ";       cout<
<<" "; }

Output: 0 0 1 1 2 2 3 3 输出:0 0 1 1 2 2 3 3

Post Increment Loop: 后增量循环:

for (int i = 0, k=0; i < 4; k=i++){cout<<<" ";       cout<
<<" "; }

Output: 0 0 1 0 2 1 3 2 输出:0 0 1 0 2 1 3 2

I hope the difference is clear by comparing the output. 我希望通过比较输出来清楚区别。 Point to note here is the increment/decrement is always performed at the end of the for loop and hence the results can be explained. 这里要注意的是,增量/减量始终在for循环的末尾执行,因此可以解释结果。


#4楼

I dont know for the other languages but in Java ++i is a prefix increment which means: increase i by 1 and then use the new value of i in the expression in which i resides, and i++ is a postfix increment which means the following: use the current value of i in the expression and then increase it by 1. Example: 我不知道其他语言,但是在Java ++中,i前缀增量 ,这意味着:将i加1,然后在i所驻留的表达式中使用i的新值,并且i ++后缀增量 ,这意味着:在表达式中使用i的当前值,然后将其增加1。示例:

public static void main(String [] args){    int a = 3;    int b = 5;    System.out.println(++a);    System.out.println(b++);    System.out.println(b);

} and the output is: 的输出是:

  • 4 4
  • 5 5
  • 6 6

#5楼

The question is: 问题是:

Is there a difference in ++i and i++ in a for loop? for循环中的++ i和i ++有区别吗?

The answer is: No . 答案是:

Why does each and every other answer have to go into detailed explanations about pre and post incrementing when this is not even asked? 为什么甚至在没有问到的情况下,每个其他答案都必须对前后递增进行详细说明?

This for-loop: 这个for循环:

for (int i = 0; // Initialization     i < 5;     // Condition     i++)       // Increment{   Output(i);}

Would translate to this code without using loops: 无需使用循环即可转换为以下代码:

int i = 0; // InitializationloopStart:if (i < 5) // Condition{   Output(i);   i++ or ++i; // Increment   goto loopStart;}

Now does it matter if you put i++ or ++i as increment here? 现在,将i++++i作为增量在这里有关系吗? No it does not as the return value of the increment operation is insignificant. 不可以,因为增量操作的返回值无关紧要。 i will be incremented AFTER the code's execution that is inside the for loop body. 在for循环体内执行代码后, i将递增。


#6楼

It boggles my mind why so may people write the increment expression in for-loop as i++. 这让我感到困惑,为什么人们可能会像i ++一样在for循环中编写增量表达式。

In a for-loop, when the 3rd component is a simple increment statement, as in 在for循环中,当第三个组件是一个简单的增量语句时,如

for (i=0; i

or 要么

for (i=0; i

there is no difference in the resulting executions. 执行的结果没有差异。

转载地址:http://pwjnb.baihongyu.com/

你可能感兴趣的文章