Next: switch
Up: Control Structures
Previous: for
while (ch != 'Q') {
process();
cin >> ch;
}
while
is more intuitive than for
in some cases but you
have to initialize variables separately. Compare:
for (double x = 100; x > 2; x = sqrt(x))
cout << x << endl;
double x = 100;
while (x > 2) {
cout << x << endl;
x = sqrt(x);
}
- continue statements are not-so-great programming style
but may be convenient in some cases
- break statements can often be avoided by using the
do {...} while(condition)
construct instead.
Andreas Koeller
2000-06-04