Chapter 4 Control Structures: Part 1 111 Note (Web server)
Chapter 4 Control Structures: Part 1 111 Note that a value is input both before reaching the loop and at the end of the loop s body. As we enter the loop, the value input before the loop is tested to determine whether it is the sentinel. If so, the loop terminates; otherwise, the body of the loop executes. The body processes the grade, then inputs the next grade. Then, the new grade is tested at the top of the loop to determine if that grade is the sentinel. The pseudocode statement Calculate and print the class average may be refined as follows: If the counter is not equal to zero Set the average to the total divided by the counter Print the average Else Print No grades were entered We test for the possibility of division by zero a logic error that, if undetected, causes the program to produce invalid output. The complete second refinement of the pseudocode algorithm for the class-average problem is shown in Fig. 4.8. Testing and Debugging Tip 4.4 When performing division by an expression whose value could be zero, explicitly test for this case and handle it appropriately in your program, possibly printing an error message. Good Programming Practice 4.5 Include blank lines in pseudocode programs for increased readability. The blank lines separate pseudocode control structures and the program s phases. Initialize total to zero Initialize counter to zero Input the first grade (possibly the sentinel) While the user has not as yet entered the sentinel Add this grade into the running total Add one to the grade counter Input the next grade (possibly the sentinel) If the counter is not equal to zero Set the average to the total divided by the counter Print the average Else Print No grades were entered Fig. 4.8 Pseudocode algorithm that uses sentinel-controlled repetition to solve the class-average problem.
Note: In case you are looking for affordable webhost to host and run your servlet application check Vision ecommerce web hosting services