3. The Handy, All-Purpose “for” Statement

Some tasks are so common that C++ provides special syntax just to represent them with fewer keystrokes: for example, the increment operator (++). Because adding 1 is so common, C++ provides this operator even though you could get away without it.

n++;    // Add 1 to n.

Another case is the for statement. Its only purpose in life is to make certain kinds of while loops more concise. But this turns out to be so useful that programmers come to rely on it heavily. I use it throughout the rest of this book.

You’ll find that once you use it a few times, the for statement will become second nature. But it looks strange the first time you see it.

Loops Used for Counting

As you worked with while loops in Chapter ...

Get C++ Without Fear: A Beginner’s Guide That Makes You Feel Smart, Second Edition now with O’Reilly online learning.

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