Ive been messing around with loops lately and I noticed how strangely for loops behave in a while loop.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
void ForLoop1() {
for(int i = 0; i < 100; i++) cout << "1looping" << i << endl;
}
void ForLoop2() {
for(int i = 0; i < 100; i++) cout << "2looping" << i << endl;
}
int main() {
while (clock() != -1) {
ForLoop1();
ForLoop2();
}
return 0;
}
The while loop completely ignores the second for loop, thus repeatedly loading the first for loop. Although this works if I convert all loops to while. This logic confuses me. I would appreciate an explanation for this weird behavior. And if possible, how to solve it with out converting all the sub loops to while.
that's not true, you've just not initialised 'i' in your for loop so its probably entering ForLoop1() only once and checking an uninitialised variable against 100, and looping forever
EDIT - I'm surprised you dont get either a crash or some kind of run-time check error, what compiler are you using?
And what do you mean I didn't initialize i? Doesn't int i = 0; "initialize" it?
A1: I'm using code blocks.
A2: I guess it wasn't the best choice of words :S