< previous page page_207 next page >

Page 207
Better yet, you could simply leave off the else part. The resulting statement is the If-Then form of the If statement. This is its syntax template:
IfStatement (the If-Then form)
if ( Expression ) Statement

Here's an example of an If-Then. Notice the indentation and the placement of the statement that follows the If-Then.
if (age < 18)
    cout << Not an eligible ;
cout << voter. << endl;
This statement means that if age is less than 18, first print Not an eligible and then print voter. If age is not less than 18, skip the first output statement and go directly to print voter. Figure 5-5 shows the flow of control for an If-Then.
Like the two branches in an If-Then-Else, the one branch in an If-Then can be a block. For example, let's say you are writing a program to compute income taxes. One of the lines on the tax form says, Subtract line 23 from line 17 and enter result on line 24; if result is less than zero, enter zero and check box 24A. You can use an If-Then to do this in C++:
0207-01.gif
Figure 5-5
If-Then Flow of Control

 
< previous page page_207 next page >