6.4 Looping Techniques
The while loop is an important tool for performing many common programming operations on a set of input values. For example, the while loop can be used to calculate the sum of values, count the number of values, find the average value, find the minimum and maximum values, and perform other operations.
6.4.1 Accumulation
Let’s look at a common programming operation for which a while loop is useful: calculating the sum of a set of values. To do this, we will build a simple calculator that performs one function: addition. We will prompt the user for numbers one at a time. We’ll make the sentinel value a 0; that is, when the user wants to stop, the user will enter a 0. At that point, we will print the total.
The calculator ...
Get Java Illuminated, 5th 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.