O'Reilly logo

C++ Without Fear: A Beginner’s Guide That Makes You Feel Smart, Second Edition by Brian Overland

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required