The (Almost) Infinite Loop, for (){}...

In the following example, why does the code execute 200 times if I enter 1000? Shouldn't the for loop die, when I enter 1000 (1000>x), or does it view all variables (excluding the one included in the actual condition), before "for(){}" as constant? this is so confusing. Does "while" behave the same way, setting all prior variable as constant? I don't think so because I've tested it before.

bla bla bla...
int main()
{
int x, y;
y=200;
for (int x; x<y; x++)
{
cin>>y; cout<<"hell yea\n";
}
return 0;
}
Last edited on
You haven't set x (either one of the two) to any value. Why would you think it was less than the number you enter for y?
You didn't initialize x anywhere. There's no way of knowing what that value is.

My compiler actually doesn't let me compile this because you are using an uninitialized variable before you assign a value to it. Your program's behavior is considered to be 'undefined'.
Yea, you're right, it works fine now. I must have messed something up in the code the other day. I was trying to emulate something, which I did then. It's so easy to make mistakes with this stuff... next time i'll post the code, after I make it.

Sorry about that guys.
Topic archived. No new replies allowed.