Java Break Statements

Anonymous
timer Asked: Apr 8th, 2017

Question description

The selection talks about break statements.

Read this section and do some research to see other uses for the break statement.

Please answer the following questions:

What are some of these other uses?

What can happen if you don't use a break statement?

How about using a break in a switch statement, show how you can really have a problem if you skip the break statement in a switch.

Thank you in advance.

THE SELECTION

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 terminates the switch statement and transfers control to the next statement after the switch statement. In addition, 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 12.9 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 shortterm stock 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. Before examining the break statement in Figure 12.9, look at the (day - 1) argument in the final 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 subtract 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.

Tutor Answer

(Top Tutor) Studypool Tutor
School: New York University
Studypool has helped 1,244,100 students
flag Report DMCA
Similar Questions
Hot Questions
Related Tags

Brown University





1271 Tutors

California Institute of Technology




2131 Tutors

Carnegie Mellon University




982 Tutors

Columbia University





1256 Tutors

Dartmouth University





2113 Tutors

Emory University





2279 Tutors

Harvard University





599 Tutors

Massachusetts Institute of Technology



2319 Tutors

New York University





1645 Tutors

Notre Dam University





1911 Tutors

Oklahoma University





2122 Tutors

Pennsylvania State University





932 Tutors

Princeton University





1211 Tutors

Stanford University





983 Tutors

University of California





1282 Tutors

Oxford University





123 Tutors

Yale University





2325 Tutors