Page 490 - Introduction to Programming with Java: A Problem Solving Approach
P. 490

                456 Chapter 11 Type Details and Alternate Coding Mechanisms
JVM then returns to the loop header and executes the stdIn.next() method call again. The JVM waits for the user to enter another value. But the user won’t know he/she is supposed to enter anything because there’s no prompt. That’s a particularly nasty logic error because the program produces no erroneous output and no error message. That means no help in determining what to do.
You can produce these same types of logic errors by putting semicolons after “if,” “else if,” or “else” headings. Such semicolons effectively create empty statements, and they’re often introduced accidentally during program development or debugging. Be on the alert for empty statements, and when- ever you see one, be suspicious and check it out! Better yet, minimize confusion at the end
by maximizing care at the beginning.
11.11 break Statement within a Loop
This section supplements the loop material you studied in Chapter 4.
In Chapter 4 we introduced you to the use of the break statement inside a switch statement. It termi-
nates the switch statement and transfers control to the next statement after the switch statement. In addi- tion, you can use the break statement inside a while, do, or for loop. It does the same thing as when it’s in a switch statement. The break terminates the immediately enclosing loop and transfers control to the next statement after the bottom of the loop. We say “immediately enclosing” because you can have a break that’s nested inside multiple loops. The break gets associated with the loop that immediately surrounds it.
The DayTrader program in Figure 11.10 illustrates what’s called “day trading.” It’s a form of gambling in which people buy and sell stock on the stock market every day in hopes of making money off short-term stock
BeforeexaminingthebreakstatementinFigure11.10,lookatthe(day - 1)argumentinthefinal printf statement. This is after the for loop, so the scope of day needs to be bigger than the scope of the for loop. That’s why we declared it before the for loop with the other local variables. But why did we sub- tract 1 in the printf statement? Because the day++ operation in the third compartment of the for loop header increments day one extra time, after the transaction that drives the balance to a terminating value. If we had forgotten to subtract 1 in the printf statement, that would be an off-by-one error.
Now look at the DayTrader program’s break statement. If the balance ever gets outside the $1 to $5,000 range, program control jumps immediately to the next statement below the for loop. If you run the program several times, you’ll see that sometimes this causes the loop to terminate before day reaches 90. You’ll get a different result each time you run the program because this program uses Math.random to generate a random number in the range between 0.0 and 1.0.
Be aware that you never really have to use a break statement to implement this premature loop- termination capability. For example, you can eliminate the DayTrader program’s if and break statements by changing the for loop header to this:
for (day=1; day<=90 && !(balance < 1.0 || balance > 5000.0); day ++)
Don’t fall into the trap of using the break statement too often. Usually, someone reading your program will look only at the loop header to figure out how the loop terminates. In using a break statement, you force
                Haste makes waste.
  Apago PDF Enhancer
movements. This program keeps track of a day trader’s stock balance over a three-month period (for day 􏰂 1 to 90). The original balance is $1,000. In our simple model, at the beginning of each day, the day trader retains half the initial balance in savings and invests the other half in the stock market. The money returned at the end of the day equals the investment times a random number between 0 and 2. Thus, the money returned ranges anywhere from zero to double the original investment. Each day, the day trader adds the money returned to the balance in savings. If the balance ever goes below $1 or above $5,000, the day trader quits.


















































































   488   489   490   491   492