Page 161 - Introduction to Programming with Java: A Problem Solving Approach
P. 161

                Here’s how the do loop works:
1. Execute the do loop’s body.
2. Check the final condition.
3. If the condition is true, jump back to the top of the do loop and repeat step 1.
4. If the condition is false, continue with the statement immediately below the loop.
Practice Problem
Now let’s illustrate the do loop with an example problem. Suppose you’re asked to write a program that prompts the user to enter length and width dimensions for each room in a proposed house so that total floor space can be calculated for the entire house. After each length/width entry, ask the user if there are any more rooms. When there are no more rooms, print the total floor space.
       To solve this problem, first ask whether a loop is appropriate. Does anything need to
be repeated? Yes, you’ll want to read in dimensions repeatedly, so a loop is appropriate. To
determine the type of loop, ask yourself: Will you always need to execute the read-in-the-
dimensions loop body at least once? Yes, every house must have at least one room, so you’ll need to read in at least one set of dimensions. Thus, it’s appropriate to use a do loop for this problem. Now that you’ve thought through the looping issues, you’re ready to put pencil to paper and write down your solution. Go for it.
When you’re done working out a solution on your own, look at our solution in Figure 4.13. Did you
prompt for length and width values within your do loop and then add the length times width product to a
4.10 for Loop 127
 How many repeats?
    total floor space variable? Did you then prompt the user for a continue decision?
Apago PDF Enhancer
Compare the loop-termination technique used in the FloorSpace program with the loop-termination technique used in the BridalRegistry program in Figure 4.11. In the BridalRegistry program, we needed two user queries—one before the start of the loop and one within the loop just before its end. In the FloorSpace program, we need only one user query—within the loop just before its end. The do loop requires that there be at least one pass, but if this is acceptable, it requires fewer lines of code than the while loop.
Before leaving the FloorSpace program, take note of a style feature. Do you see the blank lines above and below the do loop? It’s good style to separate logical chunks of code with blank lines. Since a loop is a logical chunk of code, it’s nice to surround loops with blank lines unless the loop is very short, that is, less than about four lines.
4.10 for Loop
Now let’s consider a third type of loop—the for loop. A for loop is appropriate when you know the exact number of loop iterations before the loop begins. For example, suppose you want to perform a countdown from 10, like this:
Sample session:
10 9 8 7 6 5 4 3 2 1 Liftoff!
In your program, you’ll need to print 10 numbers, and you should print each number with the help of a print statement inside a loop. Since the print statement should execute 10 times, you know the exact number of iterations for the loop, 10. Therefore, you should use a for loop.
  










































































   159   160   161   162   163