Mixed loops?!

closed account (DGvMDjzh)
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.
Last edited on
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?

EDIT 2 - Interracial?
Last edited on
closed account (DGvMDjzh)
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
Doesn't int i = 0; "initialize" it?
It does. But line 2/7 doesn't.

A2: I guess it wasn't the best choice of words :S
You might take a deeper look at your dictionary
Topic archived. No new replies allowed.