go to previous page   go to home page   go to next page

Answer:

Yes; each iteration of the loop will add a new number to the sum.

Adding up User-entered Integers

sentinel loop

 

 

The loop will add each integer the user enters to a sum. A counting loop could do this if the user first said how many integers were in the list. But often users don't know this in advance, or would find it annoying to find out. (If you have a column of integers, you would like to enter them one by one until you reach the last one. It would be annoying if you were required to count them in advance.)

The idea of a sentinel controlled loop is that there is a special value (the sentinel) that is used to say when the loop is done. In this example, the user enters a zero when the sum is complete. Zero is the sentinel. Here is how the program should work:

C:/users/temp>java AddUpNumbers

Enter first integer (enter 0 to quit): 12
Enter an integer (or 0 to quit): -3
Enter an integer (or 0 to quit): 4
Enter an integer (or 0 to quit): 0
Sum of the integers: 13

The flowchart shows the idea. It looks much like other flowcharted loops, but now the gatekeeper is testing for a sentinel value.

QUESTION 2:

Does the program itself determine how many times the loop executes?