The if Statement

When a C++ program must choose whether or not to take a particular action, you can use the if statement. The if comes in two forms: if and if else. Let's investigate the simple if first. It's modeled after ordinary English, as in "If you have a Captain Cookie card, you get a free cookie." The if statement directs a program to execute a statement or statement block if a test condition is true and to skip that statement or block if the condition is false. The syntax is similar to the while syntax:

if (test-condition)
    statement
				

A true or nonzero test-condition causes the program to execute statement, which can be a single statement or a block. A false or zero test-condition causes the program to skip statement. (See Figure 6.1 ...

Get The Waite Group's C++ Primer Plus, 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.