Cheat Sheets are in BETA. Give us your feedback.

while Loop

In C++, a while loop statement repeatedly executes the code block within as long as the condition is true. The moment the condition becomes false, the program will exit the loop.

Note that the while loop might not ever run. If the condition is false initially, the code block will be skipped.

while (guess != 8) { std::cout << "Try again: "; std::cin >> guess; }

for Loop

In C++, a for loop executes a code block a specific number of times. It has three parts:

  • The initialization of a counter
  • The continue condition
  • The increment/decrement of the counter

This example prints 0 to 9 on the screen.

for (int i = 0; i < 10; i++) { std::cout << i << "\n"; }