Need to understand for loop better - post increment operator

不羁的心 提交于 2021-02-08 12:20:02

问题


I was not really clear with the post increment operator that I always used with for loops. My latest and newly acquired understanding of post increment operator is the following:

int a = 5
int b = a++ //a will increment and return back its old value 5
so b = 5

Armed with this new knowledge i decided to understand/apply it to the places where i commonly used the post increment operator as in a for loop . Now it seems like I am lost since I am ending up with the wrong output theoretically

Consider the following code

for(int i=0 ; i< 3 ; i++)
{
  std::cout << i; 
}

First loop

i starts with 0 which is less than 3 so ( increment by 1 however since its i++ it returns old value 0) 
so cout should display 1 // But it displays 0

Second Loop

 i is now 1 which is less than 3 so i++ is applied - Now i is 2 and returns back 1
 so cout should display 2 //But it display 1

Third Loop

 i is now 2 which is less than 3 so i++ is applied - Now i is 3 and returns back 2
 so cout should display 3 //But it display 2

Fourth Loop

 i is now 3 which is not less than 3 so loop exits

Could anyone please clear my understanding and point me in the right direction. The output should be 0,1,2 where am i going wrong ?


回答1:


What you're missing is when each of those sections of the for statement happen:

for (int i = 0 ; i < 3 ; i++)
//   111111111   22222   333
  • The first bit happens once before any iterations are done.
  • The second expression is evaluated before each potential iteration and, if false, no further iterations are done.
  • The third bit is done at the end of each iteration, before returning to evaluate the second bit.

Now re-read that last bullet point carefully. The i++ is done at the end of an iteration, after the cout << i. And, immediately after that, the continuation condition is checked (the second part).

So the loop is effectively the same as:

{   // Outer braces just to limit scope of i, same as for loop.
    int i = 0;
    while (i < 3) {
        cout << i;
        i++;
    } 
}

That's why you get 0 1 2.




回答2:


The semicolons in a for loop delimit three different expressions. The value of the third expression is irrelevant to the behavior of the loop. You could replace i++ with ++i and your loop would behave the same; either way, i is incremented by 1.

To observe the behavior of the increment operators, consider the following loops:

for(int i = 0 ; i++ < 3 ; ) cout << i;
/*                      ^^^ Nothing here!                      */

Note that the third expression is empty. The output is 123—0 is skipped because the test, including increment, occurs before every iteration of the loop.

How does the postincremented loop behave compared to a similar one with the preincrement operator?

for(int i = 0 ; ++i < 3 ; ) cout << i;

Now the output is 12—3 is not printed because the conditional test sees the value 3 as soon as it is incremented to that, so the loop exits.




回答3:


You just need to know that the statement int i = 0 is executed first , i.e , i takes first the value 0 , checks if the condition is true and then what the loop body is executed .

Then , i is incremented.



来源:https://stackoverflow.com/questions/26034374/need-to-understand-for-loop-better-post-increment-operator

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!