Skip to main content

5.6) While Loops


Flow of Control


Example

int n = 10;
while (n > 0) {
  cout << n << '\n';
  n--;
}
cout << "Lift-off!" << endl;

Key Points

  • Statements in the loop body execute while the test in ( ) evaluates to true
  • { } of the loop body can be omitted if the loop body consists of a single statement
  • The loop will not execute if the test is false initially
  • It must be possible for test to become false eventually - otherwise the loop will never terminate! (In a program run from a terminal window, these 'infinite loops' can be interrupted using Ctrl+C.)