9. Control Structures

9.9. Principles of Loop Design

Before moving on, it will be useful to summarize the main principles involved in correctly constructing a loop.

 

A counting loop can be used whenever you know in advance exactly how many iterations are needed. Java’s for statement is an appropriate structure for coding a counting loop.

 

SECTION 6.9 Principles of Loop Design271

 

 

 

,

mport j ava . io . ;

ublic c l a s s Validate// C o n s o l e i n p u t

private KeyboardReader reader = new KeyboardReader ( ) ;

 

private double getAndValidateGrade ( )

double grade = 0 ;

do {r eader . prompt ( Input a grade ( e . g . , 8 5 . 3 ) +

” or 9999 to i n d i c a t e the end of the l i s t >> ) ; grade = reader . getKeyboardDouble ( ) ;

i f ( ( grade ! = 9999 ) && ( ( grade < 0 )( grade > 1 0 0 ) ) ) // I f e r r o r

System . out . p r i n t l n ( Error : grade must be between 0 and 100n” ) ;

e ls e

System . out . p r i n t l n ( ”You input + grade + \n” ) ;

/ C o n f i r m i n p u t

while ( ( grade ! = 9999 ) && ( ( grade < 0 )( grade > 1 0 0 ) ) ) ;

return grade ;

}p ublic double inputAndAverageGrades ( )

double running Total = 0 ;

in t count = 0 ;

double grade = getAndValidateGrade ( ) ;// I n i t i a l i z e : p r i m i n g i n p u t

while ( grade ! = 9999 )// L o o p t e s t : s e n t i n e l

running Total += grade ; count ++;

grade = getAndValidateGrade ( ) ;// U p d a t e : g e t n e x t g r a d e

} // w h i l e

i f ( count > 0 )// G u a r d a g a i n s t d i v i d e by z e r o

return running Total / count ;// R e t u r n t h e a v e r a g e

e ls e

return 0 ;// S p e c i a l ( e r r o r ) r e t u r n v a l u e

}p ublic s t a t i c void main ( S t r i n g argv [ ] )

System . out . p r i n t l n ( This program c a l c u l a t e s average grade . ) ; // E x p l a i n

Average avg = new Average ( ) ;

double average = avg . inputAndAverageGrades ( ) ;

i f ( average == 0 )// E r r o r c h e c k

System . out . p r i n t l n ( ”You didn t enter any grades . ) ;

e ls e

System . out . p r i n t l n ( ”Your average i s + average ) ;

} // m a i n ( )

// V a l i d a t e

J

Figure 6.12: A program to compute average grade using a while struc- ture. This version validates the user’s input.

 

A while structure should be used when the problem suggests that the loop body may be skipped entirely. Java’s while statement is specially designed for the while structure.

A do-while structure should be used only when a loop requires one or more iterations. Java’s do-while statement is specially designed for the do-while structure.

The loop variable is used to specify the loop-entry condition. It must be initialized to an appropriate initial value, and it must be updated on each iteration of the loop.

A loop’s bound may be a count, a sentinel, or, more generally, a conditional bound. It must be correctly specified in the loop-entry expression, and progress toward the bound must be made in the updater.

An infinite loop may result if the initializer, loop-entry expression, or updater expression is not correctly specified.

 

 

The loop types are also summarized in Table 6.1.

 

TABLE 6.1 A summary of the design decisions required when coding a loop

 

Use

If

Java Statement

Counting loop

Number of iterations known in advance

for

While structure

Number of iterations not known Loop may not be entered at all

while

Do-while structure

Number of iterations not known Loop must be entered at least once

do-while

 

 

 

 

 

SELF-STUDY EXERCISE

 

EXERCISE 6.14 For each of the following problems, decide whether a counting loop structure, a while structure, or a do-while structure should be used, and write a pseudocode algorithm.

Print the names of all visitors to your Web site.

Validate that a number input by the user is positive.

Change all the backslashes ( ) in a Windows Web page address to the slashes (/) used in a Unix Web page address.

 

Find the car with the best miles-per-gallon ratio among the cars in the Consumer Reports database.