Question: Writing a Loop This How To walks you through the process of implementing a loop statement. We will illustrate the steps with the following example

 Writing a Loop This How To walks you through the processof implementing a loop statement. We will illustrate the steps with thefollowing example problem. Problem Statement Read twelve temperature values (one for eachmonth) and display the number of the month with the highest temperature.

Writing a Loop This How To walks you through the process of implementing a loop statement. We will illustrate the steps with the following example problem. Problem Statement Read twelve temperature values (one for each month) and display the number of the month with the highest temperature. For example, according to http://worldclimate.com, the average maximum temperatures for Death Valley are (in order by month, in degrees Celsius): 18.222.626.431.136.642.2 In this case, the month with the highest temperature (45.7 degrees Celsius) is July, and the program should display 7. Decide what work must be done inside the loop. Every loop needs to do some kind of repetitive work, such as - Reading another item. - Updating a value (such as a bank balance or total). - Incrementing a counter. If you can't figure out what needs to go inside the loop, start by writing down the steps that you would take if you solved the problem by hand. For example, with the temperature reading problem, you might write Read the first value. Read the second value. If the second value is higher than the first value set highest temperature to the second value. Set highest month to 2. Read the next value. If the value is higher than the first and second values set highest temperature to the value. set highest month to 3. Read the next value. If the value is higher than the highest temperature seen so far set highest temperature to the value. Set highest month to 4. ... Now look at these steps and reduce them to a set of uniform actions that can be placed into the loop body. The first action is easy: Read the next value. The next action is trickier. In our description, we used tests "higher than the first", "higher than the first and second", "higher than the highest temperature seen so far". We need to settle on one test that works for all iterations. The last formulation is the most general. Similarly, we must find a general way of setting the highest month. We need a variable that stores the current month, running from 1 to 12 . Then we can formulate the second loop action: If the value is higher than the highest temperature set highest temperature to the value. set highest month to current month. Altogether our loop is While... Read the next value. If the value is higher than the highest temperature set the highest temperature to the value. set highest month to current month. increment current month. Step 2 Specify the loop condition. What goal do you want to reach in your loop? Typical examples are - Has a counter reached its final value? - Have you read the last input value? - Has a value reached a given threshold? In our example, we simply want the current month to reach 12. Step 3 Determine the loop type. We distinguish between two major loop types. A count-controlled loop is executed a definite number of times. In an event-controlled loop, the number of iterations is not known in advance - the loop is executed until some event happens. Count-controlled loops can be implemented as for statements. For other loops, consider the loop condition. Do you need to complete one iteration of the loop body before you can tell when to terminate the loop? In that case, choose a do loop. Otherwise, use a while loop. Sometimes, the condition for terminating a loop changes in the middle of the loop body. In that case, you can use a Boolean variable that specifies when you are ready to leave the loop. Follow this pattern: Such a variable is called a flag. In summary, - If you know in advance how many times a loop is repeated, use a for loop. - If the loop body must be executed at least once, use a do loop. - Otherwise, use a while loop. In our example, we read 12 temperature values. Therefore, we choose a for loop. Step 4 Set up variables for entering the loop for the first time. List all variables that are used and updated in the loop, and determine how to initialize them. Commonly, counters are initialized with 0 or 1 , totals with 0. 6.7 Common Loop Algorithms 205 In our example, the variables are currentmonth highest value highest month We need to be careful how we set up the highest temperature value. We can't simply set it to 0. After all, our program needs to work with temperature values from Antarctica, all of which may be negative. A good option is to set the highest temperature value to the first input value. Of course, then we need to remember to read in only 11 more values, with the current month starting at 2 . We also need to initialize the highest month with 1. After all, in an Australian city, we may never find a month that is warmer than January. Step 5 Process the result after the loop has finished. In many cases, the desired result is simply a variable that was updated in the loop body. For example, in our temperature program, the result is the highest month. Sometimes, the loop computes values that contribute to the final result. For example, suppose you are asked to average the temperatures. Then the loop should compute the sum, not the average. After the loop has completed, you are ready to compute the average: divide the sum by the number of inputs. Here is our complete loop. Readvalue. highest temperature = value highest month =1 For current month from 2 to 12 Readnext value. If the value is higher than the highest temperature set highest temperature to the value. set highest month to current month. Step 6 Trace the loop with typical examples. Hand-trace your loop code, as described in Section 6.2. Choose example values that are not too complex-executing the loop 3-5 times is enough to check for the most common errors. Pay special attention when entering the loop for the first and last time. Sometimes, you want to make a slight modification to make tracing feasible. For example, when hand-tracing the investment doubling problem, use an interest rate of 20 percent rather than 5 percent. When hand-tracing the temperature loop, use 4 data values, not 12. Let's say the data are 22.636.644.5 24.2. Here is the walkthrough: The trace demonstrates that highest month and highest value are properly set. Step 7 Implement the loop in Java. Here's the loop for our example. Exercise E6.4 asks you to complete the program. double highestValue; highestValue = in. , extDouble () ; int highestMonth =1; 206 Chapter 6 Loops for (int currentMonth =2; currentMonth highestValue) {highestValue=nextValue;highestMonth=currentMonth; \} \}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!