3 The Handy, All-Purpose “for” Statement

Some tasks are so common that C++ provides special syntax just to represent them with fewer keystrokes. An example is the increment operator (++) introduced in Chapter 2. Because adding 1 to a variable is so common, C++ provides this operator to add 1 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 becomes second nature. Unfortunately, it looks strange the first time you see it. And to be ...

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

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