Modern C++: The Good Parts/Getting loopy

For this chapter, we have a calculator willing to perform more calculations than you are willing to input.

Explanation
This program will run until your terminal stops running it (because you press Ctrl+C, or click the X). That's because the while loop, every time control reaches its bottom, jumps back up to the top - if its condition is still true, which it always will be in this case.

If we weren't in a context where the user could easily halt the loop, the  would be a very bad idea. Instead of, you can put any Boolean expression (like  ) or   variable between the parentheses.

Suppose we wanted to allow up to ten iterations (passes through the loop) instead of an unlimited number. We could do this: And that would work just fine.

is the same thing as, and results in   being one greater than it was. It's also the same as.

But C++ provides a prettier way to write that, the for loop: For the first iteration,  will have the value 0. For the last iteration, it will have the value 9. It will never (usefully) have the value 10 because, just like with our equivalent while loop above: Nonetheless, the loop does run 10 times.
 * 1) the iteration runs;
 * 2) then   is incremented;
 * 3) then   is checked, and if it's false the loop is exited.

Exercises
under construction

Vocabulary

 * while loop: continues looping until its condition is false.
 * iteration: one pass through a loop. Also, the action of looping or "iterating".
 * for loop: more suited to counting than a while loop is.