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 3I 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.
精彩评论