4.3. if Selection Statement

Programs use selection statements to choose among alternative courses of action. For example, suppose the passing grade on an exam is 60. The statement

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

determines whether the condition grade >= 60 is true or false. If the condition is true, "Passed" is printed and the next statement in order is performed. If the condition is false, the output statement is ignored and the next statement in order is performed. The indentation of the second line is optional, but it’s recommended because it emphasizes the inherent structure of the program.

It’s important to note here that we’re casually assuming that grade contains a valid value—an integer in the range 0 to 100. Throughout the ...

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.