Cs219 - how about using a break in a switch statement show


Include at least 1 URL reference to resources used (APA format)

Needs to be 2 paragraphs long.

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

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.
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.

Attachment:- Java.rar

Solution Preview :

Prepared by a verified Expert
JAVA Programming: Cs219 - how about using a break in a switch statement show
Reference No:- TGS02383055

Now Priced at $25 (50% Discount)

Recommended (93%)

Rated (4.5/5)