4.4. if...else Double-Selection Statement

The if single-selection statement performs an indicated action only when the condition is true; otherwise the action is skipped. The if...else double-selection statement allows you to specify an action to perform when the condition is true and a different action to perform when the condition is false. For example, the statement

if ( grade >= 60 )    cout << "Passed"; else    cout << "Failed";

displays "Passed" if grade >= 60, but displays "Failed" if the student’s grade is less than 60. In either case, the next statement in sequence is then performed. The body of the else is also indented. Figure 4.4 illustrates the the if...else statement’s flow of control.

Fig. 4.4. if...else double-selection statement ...

Get C++11 for Programmers, Second Edition now with O’Reilly online learning.

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