|
|
|
|
|
|
|
if (test1 >= 0 && test2 >= 0 && test3 >= 0)
.
.
. |
|
|
|
|
|
|
|
|
To convince yourself that these three variations work, try them by hand with some test data. |
|
|
|
|
|
|
|
|
If these statements do the same thing, how do you choose which one to use? If your goal is efficiency, the compound condition in the If statement is best. If you are trying to express as clearly as possible what your code is doing, the longer form shown in the program may be best. The other variations lie somewhere in between. (However, some people would find the compound condition in the If statement to be not only the most efficient but also the clearest to understand.) There are no absolute rules to follow here, but the general guideline is to strive for clarity, even if you must sacrifice a little efficiency. |
|
|
|
|
|
|
|
|
Problem-solving case study The Lumberyard |
|
|
|
|
|
|
|
|
Problem: You've been hired by a local lumberyard to help computerize its operations. Your first assignment is to write a program that computes the total amount of an item ordered in standard units, given its type, dimensions, and the number of pieces ordered. The yard sells two kinds of items: dimensioned lumber and plywood panels. First, the program should read a lettereither L for lumber or P for plywoodto determine the type. Next, it should read four integer numbers. For lumber, the first two numbers are the width and depth in inches; the third number is the length in feet; and the fourth number is the number of pieces ordered. So |
|
|
|
|
|