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 book, we’ll introduce many important validation techniques.


Image Error-Prevention Tip 4.1

In industrial-strength code, always validate all inputs.


Figure 4.3 illustrates the single-selection if statement. It contains what is perhaps the most important symbol in an activity diagram—the diamond or decision symbol, which indicates that a decision is to be made. A decision symbol indicates that the workflow will continue along a path determined by the symbol’s associated guard conditions, which can be true or false. Each transition arrow emerging from a decision symbol has a guard condition specified in square brackets above or next to the transition arrow. If a particular guard condition is true, the workflow enters the action state to which that transition arrow points. In Fig. 4.3, if the grade is greater than or equal to 60, the program prints “Passed” to the screen, then transitions to the final state of this activity. If the grade is less than 60, the program immediately transitions to the final state without displaying a message.

Image

Fig. 4.3. if single-selection statement activity diagram.

You saw in Chapter 2 that decisions can be based on conditions containing relational or equality operators. Actually, in C++, a decision can be based on any expression—if the expression evaluates to zero, it’s treated as false; if the expression evaluates to nonzero, it’s treated as true. C++ provides the data type bool for variables that can hold only the values true and false—each of these is a C++ keyword.


Image Portability Tip 4.1

For compatibility with earlier versions of C, which used integers for Boolean values, the bool value true also can be represented by any nonzero value (compilers typically use 1) and the bool value false also can be represented as the value zero.


The if statement is a single-entry/single-exit statement. We’ll see that the activity diagrams for the remaining control statements also contain initial states, transition arrows, action states that indicate actions to perform, decision symbols (with associated guard conditions) that indicate decisions to be made and final states.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.224.61.12