开发者

while infinite loop?

I came across this question in this forum

#include <iostream>

using namespace std;

int main(int argc, char** argv) {

    int x=0;
    while (x<3) {
        x = x++;
        cout << x << endl;
    }

    return 0;
}

given the code above, why is the while loop infinite? Using gcc 4.4 under mac os, the while loop does ter开发者_JS百科minate :) so the question does not apply for all architectures. The output I get tough is

1

2

3

I don't see 0, and I guess the reason is related to the double assignment?


x = x++;

is undefined behavior


you never see zero because the increment is before the cout.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜