|
|
|
|
|
|
|
Figure 5-4
Flow of Control for Calculating Pay |
|
|
|
|
|
|
|
|
abovethe worker's pay and the division-by-zero examplesthere seems to be a semicolon at the end of each If statement. However, the semicolons belong to the statements in the else-clauses in those examples; assignment statements end in semicolons, as do output statements. The If statement doesn't have its own semicolon at the end. |
|
|
|
|
|
|
|
|
Blocks (Compound Statements) |
|
|
|
|
|
|
|
|
In our division-by-zero example, suppose that when the divisor is equal to zero we want to do two things: print the error message and set a variable named result equal to a special value like 9999. We would need two statements in the same branch, but the syntax template seems to limit us to one. |
|
|
|
|
|
|
|
|
What we really want to do is turn the else-clause into a sequence of statements. This is easy. Remember from Chapter 2 that the compiler treats the block (compound statement) |
|
|
|
|
|
|
|
|
like a single statement. If you put a { } pair around the sequence of statements you want in a branch of the If statement, the sequence of statements becomes a single block. For example: |
|
|
|
|
|