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 increments 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 ...

Get Java™ How To Program (Early Objects), Tenth Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.