Previous: 2.5 More C Statements
Up: 2.5 More C Statements
Next: 2.5.2 Simple Compiler Directives
Previous Page: 2.5 More C Statements

2.5.1 Making Decisions with Branches

Suppose there are different pay scales for regular and overtime work, so there are alternate ways of calculating pay: regular pay and overtime pay. Our next task is to write a program that calculates pay with work over 40 hours paid at 1.5 times the regular rate.

Task

PAY2: Same as PAY1, except that overtime pay is calculated at 1.5 times the normal rate.

For calculating pay in alternate ways, the program must make decisions during execution; so, we wish to incorporate the following steps in our algorithm:

if hours_worked is greater than 40.0,
          then calculate pay as the sum of
               excess hours at the overtime rate plus
               40.0 hours at regular rate;
          otherwise, calculate pay at the regular rate.
The program needs to make a decision: is hours_worked greater than 40.0? If so, execute one computation; otherwise, execute the alternate computation. Each alternate computation is implemented as a different path for program control flow to follow, called a branch. C provides a feature for implementing this algorithm form as follows:
if (hours_worked > 40.0)
          pay = 40.0 * rate_of_pay +
                    1.5 * rate_of_pay * (hours_worked - 40.0);
     else
          pay = hours_worked * rate_of_pay;
The above if statement first evaluates the expression within parentheses:
hours_worked > 40.0
and if the expression is True, i.e. hours_worked is greater than 40.0, then the first statement is executed. Otherwise, if the expression is False, the statement following the else is executed. After one of the alternate statements is executed, the statement after the if statement will be executed. That is, in either case, the program control passes to the statement after the if statement.

The general syntax of an if statement is:

The keyword if and the parentheses are required as shown. The two <statement>s shown are often called the then clause and the else clause respectively. The statements may be any valid C statement including a simple statement, a compound statement (a block), or even an empty statement. The else clause, the keyword else followed by a <statement>, is optional. Omitting this clause is equivalent to having an empty statement in the else clause. An if statement can be nested, i.e. either or both branches may also be if statements.

The semantics of the if statement are that the expression (also called the condition) is evaluated, and the control flow branches to the then clause if the expression evaluates to True, and to the else clause (if any) otherwise. Control then continues with the statement immediately after the if statement. This control flow is shown in Figure 2.8.

It should be emphasized that only one of the two alternate branches is executed in an if statement. Suppose we wish to check if a number, , is positive and also check if it is big, say greater than 100. Let us examine the following statement:

if (x > 0)
          printf("%d is a positive number\n", x);
     else if (x > 100)
          printf("%d is a big number greater than 100\n", x);
If x is positive, say 200, the first condition is True and the first printf() statement is executed. The control does not proceed to the else part at all, even though x is greater than 100. The else part is executed only if the first condition is False. When two conditions overlap, one must carefully examine how the statement are constructed. Instead of the above, we should write:
if (x > 0)
          printf("%d is a positive number\n", x);
     if (x > 100)
          printf("%d is a big number greater than 100\n", x);
Each of the above is a separate if statement. If x is positive, the first printf() is executed. In either case control then passes to the next if statement. If x is greater than 100, a message is again printed. Another way of writing this, since (x > 100) is True only when (x > 0), we could write:
if (x > 0) {
          printf("%d is a positive number\n", x);
          if (x > 100)
               printf("%d is a big number greater than 100\n", x);
     }
If (x > 0) is true, the compound statement is executed. It prints a message and executes the if (x > 100) ... statement. Suppose, we also wish to print a message when x is negative. We can add an else clause to the first if statement since positive and negative numbers do not overlap.
if (x > 0) {
          printf("%d is a positive number\n", x);
          if (x > 100)
               printf("%d is a big number greater than 100\n", x);
     }
     else if (x < 0)
          printf("%d is a negative number\n", x);
Something for you to think about: is there any condition for which no messages will be printed by the above code?

Returning to our payroll example, suppose we wish to keep track of both regular and overtime pay for each person. We can write the if statement:

if (hours_worked > 40.0) {
          regular_pay = 40.0 * rate_of_pay;
          overtime_pay = 1.5 * rate_of_pay * (hours_worked - 40.0);
     }
     else {
          regular_pay = hours_worked * rate_of_pay;
          overtime_pay = 0.0;
     }
     pay = regular_pay + overtime_pay;
Note: both clauses in this case are compound statements; each block representing a branch is treated as a single unit. Whichever branch is executed, that entire block is executed. If hours_worked exceeds 40.0, the first block is executed; otherwise, the next block is executed. Note, both blocks compute regular and overtime pay so that after the if statement the total pay can be calculated as the sum of regular and overtime pay. Also observe that we have used consistent data types in our expressions to forestall any unexpected problems. Since variables in the expressions are float type, we have used floating point constants 40.0, 1.5, and 0.0.

Relational Operators

The greater than operator, >, used in the above expressions is called a relational operator. Other relational operators defined in C, together with their meanings are shown in Table 2.2 Note that for those relational operators having more than one symbol, the order of the symbols must be as specified in the table (>= not =>). Also take particular note that the equality relational operator is ==, NOT =, which is the assignment operator.

A relational operator compares the values of two expressions, one on each side of it. If the two values satisfy the relational operator, the overall expression evaluates to True; otherwise, it evaluates to False. In C, an expression that evaluates to False has the value of zero and an expression that evaluates to True has a non-zero value, typically 1. The reverse also holds; an expression that evaluates to zero is interpreted as False when it appears as a condition and expression that evaluates to non-zero is interpreted as True.



Previous: 2.5 More C Statements
Up: 2.5 More C Statements
Next: 2.5.2 Simple Compiler Directives
Previous Page: 2.5 More C Statements

tep@wiliki.eng.hawaii.edu
Tue Aug 16 14:01:55 HST 1994