The astute reader may recognize that we do not even need the if statement.
Consider this:
mSum = m1 + m2; //add the meters
cmSum = cm1 + cm2; //add the centimeters mSum = mSum + cmSum / 100;
cmSum = cmSum % 100;
where the last two statements come from the if statement.
We know therefore that this will work if cmSum is greater than or equal to 100 since, when that is the case, these four statements are executed.
What if cmSum is less than 100? Originally, the last two statements would not have been executed since the if condition would have been false. Now they are executed. Let us see what happens. Using the example of 3m 25cm and 2m 15cm, we get mSum as 5 and cmSum as 40.
In the next statement 40 / 100 is 0 so mSum does not change and in the last statement 40 % 100 is 40 so cmSum does not change. So the answer will be printed correctly as Sum is 5m 40cm
You should begin to realize by now that there is usually more than one way to express the logic of a program. With experience and study, you will learn which ways are better and why.
The part from if to endif is an example of the if...else construct.
The condition
average is greater than or equal to 50
is another example of a relational expression. If the condition is true, the statement after then (the then part) is executed; if it is false, the statement after else (the else part) is executed.
The whole construct is terminated with endif.
When you write pseudocode, what is important is that the logic intended is unmistakably clear. Note again how indentation can help by making it easy to identify the then part and the else part.
In the end, though, you must express the code in some programming language for it to be run on a computer. Program P4.4 shows how to do this for the above algorithm.
Program P4.4
//request 3 marks; print their average and Pass/Fail
#include <stdio.h>
int main() {
int mark1, mark2, mark3;
double average ;
printf("Enter 3 marks: ");
scanf("%d %d %d", &mark1, &mark2, &mark3);
average = (mark1 + mark2 + mark3) / 3.0;
printf("\nAverage is %3.1f", average);
if (average >= 50) printf(" Pass\n");
else printf(" Fail\n");
}
Study carefully the if...else construct in the program. It reflects the logic expressed on the previous page. Note, again, that the word then is omitted in C.
In general, the if...else construct in C takes the form shown below.
if (<condition>) <statement1> else <statement2>
The words if and else, and the brackets, are required by C. You must supply <condition>,
<statement1> and <statement2>. Each of <statement1> and <statement2> can be a one-line statement or a block. If <condition> is true, <statement1> is executed and <statement2> is skipped; if <condition> is false, <statement1> is skipped and <statement2> is executed. When the if construct is executed, either <statement1> or <statement2> is executed, but not both.
If <statement1> and <statement2> are one-line statements, you can use this layout:
if (<condition>) <statement1>
else <statement2>
If <statement1> and <statement2> are blocks, you can use the following layout:
if (<condition>) { ...
} else { ...
}
In describing the various constructs in C, we normally use the phrase “where <statement> can be a one-line statement or a block.”
It is useful to remember that, in C, for one-line statements, the semicolon is considered part of the statement. Examples are:
a = 5;
printf("Pass\n");
scanf("%d", &n);
So, in those cases where one-line statements are used, the semicolon, being part of the statement, must be present. In Program P4.4, in the if...else statement,
<statement1> is printf("Pass\n");
and <statement2> is printf("Fail\n");
However, for a block or compound statement, the right brace, }, ends the block. So, in those cases where a block is used, there is no need for an additional semicolon to end the block.
It is sometimes useful to remember that the entire if...else construct (from if to
<statement2>) is considered by C to be one statement and can be used in any place where one statement is required.
4.4.1 Calculate Pay
For an example that requires blocks, suppose we have values for hours worked and rate of pay (the amount paid per hour) and wish to calculate a person’s regular pay, overtime pay, and gross pay based on the following:
If hours worked is less than or equal to 40, regular pay is calculated by multiplying hours worked by rate of pay and overtime pay is 0. If hours worked is greater than 40, regular pay is calculated by multiplying 40 by the rate of pay and overtime pay is calculated by multiplying the hours in excess of 40 by the rate of pay by 1.5. Gross pay is calculated by adding regular pay and overtime pay.
For example, if hours is 36 and rate is 20 dollars per hour, regular pay is $720 (36 times 20) and overtime pay is $0. Gross pay is $720.
And if hours is 50 and rate is 12 dollars per hour, regular pay is $480 (40 times 12) and overtime pay is $180 (excess hours 10 times 12 times 1.5). Gross pay is $660 (480 + 180).
The above description could be expressed in pseudocode as follows:
if hours is less than or equal to 40 then set regular pay to hours x rate
set overtime pay to 0 else
set regular pay to 40 x rate
set overtime pay to (hours – 40) x rate x 1.5 endif
set gross pay to regular pay + overtime pay
We use indentation to highlight the statements to be executed if the condition “hours is less than or equal to 40” is true and those to be executed if the condition is false. The whole construct is terminated with endif.
The next step is to convert the pseudocode to C. When we do, we have to make sure that we stick to C’s rules for writing an if...else statement. In this example, we have to ensure that both the then and else parts are written as blocks since they both consist of more than one statement.
Using the variables hours (hours worked), rate (rate of pay), regPay (regular pay), ovtPay (overtime pay), and grossPay (gross pay), we write the C code, thus:
if (hours <= 40) {
regPay = hours * rate;
ovtPay = 0;
} //no semicolon here; } ends the block else {
regPay = 40 * rate;
ovtPay = (hours - 40) * rate * 1.5;
} //no semicolon here; } ends the block grossPay = regPay + ovtPay;
Note the two comments. It would be wrong to put a semicolon after the first } since the if statement continues with an else part. If we were to put one, it effectively ends the if statement and C assumes there is no else part. When it finds the word else, there will be no if with which to match it and the program will give a “misplaced else” error.
There is no need for a semicolon after the last } but putting one would do no harm.
Problem: Write a program to prompt for hours worked and rate of pay. The program then calculates and prints regular pay, overtime pay, and gross pay, based on the above description.
The following algorithm outlines the overall logic of the solution:
prompt for hours worked and rate of pay if hours is less than or equal to 40 then set regular pay to hours x rate
set overtime pay to 0
else
set regular pay to 40 x rate
set overtime pay to (hours – 40) x rate x 1.5 endif
set gross pay to regular pay + overtime pay print regular pay, overtime pay and gross pay
This algorithm is implemented as Program P4.5. All the variables are declared as double so that fractional values can be entered for hours worked and rate of pay.
Program P4.5
#include <stdio.h>
int main() {
double hours, rate, regPay, ovtPay, grossPay;
printf("Hours worked? ");
scanf("%lf", &hours);
printf("Rate of pay? ");
scanf("%lf", &rate);
if (hours <= 40) {
regPay = hours * rate;
ovtPay = 0;
} else {
regPay = 40 * rate;
ovtPay = (hours - 40) * rate * 1.5;
}
grossPay = regPay + ovtPay;
printf("\nRegular pay: $%3.2f\n", regPay);
printf("Overtime pay: $%3.2f\n", ovtPay);
printf("gross pay: $%3.2f\n", grossPay);
}
A sample run of this program is shown here:
Hours worked? 50 Rate of pay? 12 Regular pay: $480.00 Overtime pay: $180.00 Gross pay: $660.00
You should verify that the results are indeed correct.
Note that even though hours and rate are double, data for them can be supplied in any valid numeric format—here we use the integers 50 and 12. These values would be converted to double format before being stored in the variables. We could, if we wished, have typed 50.0 and 12.00, for example.