EXPLORATION 7

image

For Loops

Explorations 2 and 3 show some simple while loops. This Exploration introduces the while loop’s big brother, the for loop.

Bounded Loops

You’ve already seen while loops that read from the standard input until no more input is available. That is a classic case of an unbounded loop. Unless you know beforehand exactly what the input stream will contain, you cannot determine the loop’s bounds or limits. Sometimes you know in advance how many times the loop must run; that is, you know the bounds of the loop, making it a bounded loop. The for loop is how C++ implements a bounded loop.

Let’s start with a simple example. Listing ...

Get Exploring C++ 11, Second Edition now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.