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

Some tasks are so common that C++ provides special syntax just to represent them with fewer keystrokes: the increment operator (++) is one example. Because adding 1 is so common, C++ provides the increment operator to save space, although many other languages make do without it.

++n;    // Add 1 to n.

Subtraction by one is common as well, so C++ also provides the decrement operator, used in the expressions −−n and n−−.

Another example is the C++ for statement. Its only purpose is to make certain kinds of loops more concise. However, this turns out to be so useful that programmers rely on it heavily and I use it throughout the rest of this book.

You’ll find that once you use it a few times, the for statement ...

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

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.