The ifStatement 69
would do the trick. However, recall that if the preceding statement were used, the deci-
mal portion of the result of the division would be lost.This is because an integer division
would be performed because boththe numerator and the denominator of the division
operation are integers.
Two different solutions are possible for this problem. One is to declare either
numberOfGradesor gradeTotalto be of type float.This then guarantees that the divi-
sion is carried out without the loss of the decimal places.The only problem with this
approach is that the variables numberOfGradesand gradeTotalare used by the program
to store only integer values. Declaring either of them to be of type floatonly obscures
their use in the program and is generally not a very clean way of doing things.
The other solution, as used by the program, is to actually convertthe value of one of
the variables to a floating-point value for the purposes of the calculation.The type cast
operator (float)is used to convert the value of the variable gradeTotalto type float
for purposes of evaluation of the expression. Because the value of gradeTotalis cast
into a floating-point value beforethe division takes place, the division is treated as the
division of a floating value by an integer. Because one of the operands is now a floating-
point value, the division operation is carried out as a floating-point operation.This
means, of course, that you obtain those decimal places that you want in the average.
After the average has been calculated, it is displayed at the terminal to two decimal
places of accuracy. If a decimal point followed by a number (known collectively as a pre-
cision modifier) is placed directly before the format character f(or e) in a printfformat
string, the corresponding value is displayed to the specified number of decimal places,
rounded. So in Program 6.2, the precision modifier .2is used to cause the value of
averageto be displayed to two decimal places.
After the program has displayed the number of failing grades, execution of the pro-
gram is complete.
The if-elseConstruct
If someone asks you whether a particular number is even or odd, you most likely make
the determination by examining the last digit of the number. If this digit is either 0, 2, 4,
6, or 8, you readily state that the number is even. Otherwise, you claim that the number
is odd.
An easier way for a computer to determine whether a particular number is even or
odd is affected not by examining the last digit of the number to see if it is 0, 2, 4, 6, or
8, but by simply determining whether the number is evenly divisible by 2. If it is, the
number is even; else it is odd.
You have already seen how the modulus operator %is used to compute the remainder
of one integer divided by another.This makes it the perfect operator to use in determin-
ing whether an integer is evenly divisible by 2. If the remainder after division by 2 is
zero, it is even; else it is odd.
Look at Program 6.3—a program that determines whether an integer value typed in
by the user is even or odd and that displays an appropriate message at the terminal.