Java Reference
In-Depth Information
Program Logic for Sentinel-Controlled Repetition vs. Counter-Controlled Repetition
Line 37 declares double variable average , which allows us to store the class average as a
floating-point number. Line 14 initializes gradeCounter to 0 , because no grades have been
entered yet. Remember that this program uses sentinel-controlled repetition to input the
grades. To keep an accurate record of the number of grades entered, the program incre-
ments gradeCounter only when the user enters a valid grade.
Compare the program logic for sentinel-controlled repetition in this application with
that for counter-controlled repetition in Fig. 4.8. In counter-controlled repetition, each
iteration of the while statement (lines 17-23 of Fig. 4.8) reads a value from the user, for
the specified number of iterations. In sentinel-controlled repetition, the program reads the
first value (lines 18-19 of Fig. 4.10) before reaching the while . This value determines
whether the program's flow of control should enter the body of the while . If the condition
of the while is false, the user entered the sentinel value, so the body of the while does not
execute (i.e., no grades were entered). If, on the other hand, the condition is true , the body
begins execution, and the loop adds the grade value to the total and increments the
gradeCounter (lines 24-25). Then lines 28-29 in the loop body input the next value from
the user. Next, program control reaches the closing right brace of the loop body at line 30,
so execution continues with the test of the while 's condition (line 22). The condition uses
the most recent grade input by the user to determine whether the loop body should exe-
cute again. The value of variable grade is always input from the user immediately before
the program tests the while condition. This allows the program to determine whether the
value just input is the sentinel value before the program processes that value (i.e., adds it to
the total ). If the sentinel value is input, the loop terminates, and the program does not
add -1 to the total .
Good Programming Practice 4.3
In a sentinel-controlled loop, prompts should remind the user of the sentinel.
After the loop terminates, the if else statement at lines 34-45 executes. The con-
dition at line 34 determines whether any grades were input. If none were input, the else
part (lines 44-45) of the if else statement executes and displays the message "No
grades were entered" and the method returns control to the calling method.
Braces in a while statement
Notice the while statement's block in Fig. 4.10 (lines 23-30). Without the braces, the
loop would consider its body to be only the first statement, which adds the grade to the
total . The last three statements in the block would fall outside the loop body, causing the
computer to interpret the code incorrectly as follows:
while (grade != -1 )
total = total + grade; // add grade to total
gradeCounter = gradeCounter + 1 ; // increment counter
// prompt for input and read next grade from user
System.out.print( "Enter grade or -1 to quit: " );
grade = input.nextInt();
The preceding code would cause an infinite loop in the program if the user did not input
the sentinel -1 at line 19 (before the while statement).
 
Search WWH ::




Custom Search