Page 167 - Introduction to Programming with Java: A Problem Solving Approach
P. 167
When figuring out which loop to use, it’s best to think about the loops in the order of appearance in Fig- ure 4.16. Why? Note how the for loop uses the fewest lines, the do loop uses the next fewest lines, and the while loop uses the most lines. Thus, the for loop is the most compact and the do loop is the next most compact. But the while loop is more popular than the do loop because its condition is at the beginning of the loop, which makes it easier to find. Although you may wish to avoid the do loop because of its relatively awkward structure, in general, you should use the loop that’s most appropriate for your particular problem.
When deciding how to write loop code, you can use the templates shown in Figure 4.16 as starting points. Be aware that in writing loop code, you have to do more than just copy code from Figure 4.16. You need to adapt the code to your particular problem. For example, in writing a for loop, it’s common to use i=0 for the initialization component, and that’s why the for loop template’s initialization component shows i=0. However, if some other initialization component is more appropriate, like count=10, then use the more appropriate code.
4.12 Nested Loops
A nested loop is a loop that’s inside another loop. You’ll see nested loops quite often in real-world programs. In this section, we discuss some of the common characteristics inherent to nested loops.
Suppose you’re asked to write a program that prints a rectangle of characters where the user specifies the rectangle’s height, the rectangle’s width, and the character’s value.
4.12 Nested Loops 133
Sample session:
Enter height: 4
Enter width: 3
Apago PDF Enhancer
Enter character: <
<<<
<<<
<<<
<<<
To figure out the loops, you first need to think about what needs to be repeated. So, . . .
what needs to be repeated? You need to print rows of characters repeatedly. What type of
loop should you use to print the rows repeatedly? First try to use a for loop. The test for a
for loop is whether you know the number of times you’ll need to repeat the loop. Do you know the number of times you’ll need to repeat this loop? Yes, the user enters the height, you can use that entered value to de- termine the number of rows, and that tells you the number of times to repeat the loop. Therefore, you should use a for loop to print successive rows.
Now that you know how to print multiple rows, you need to know how to print an individual row. Do you need to repeat anything when printing an individual row? Yes, you need to print characters repeatedly. So what type of loop should you use for that? Use another for loop because you can use the user’s width entry to determine the number of characters to be printed.
So there you go—you need two for loops. Should you put one loop right after the other? No! You need to nest the second loop, the one that prints an individual row, inside the first loop. That should make sense if you word the goal carefully—“Print multiple rows and within each row, print a sequence of characters.” The key word is “within.” That tells you to insert the second for loop inside the first for loop’s braces.
Using this discussion as a guideline, now write a complete program solution. When you’re done, com- pare your answer to the NestedLoopRectangle program in Figure 4.17.
Select the best tool for the job.