How about using a break in a switch statement

Assignment Help JAVA Programming
Reference no: EM131587512

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

Reference no: EM131587512

Questions Cloud

Provides construction equipment-trailers and crutches : Round Table Rental Yards provides construction equipment, trailers, crutches, etc., on short-term rentals.
Research the disclosure of postretirement health care : Using the Internet, go to the FASB Website, and other resources to research the Disclosure of Postretirement Health Care and Life Insurance Benefits
What is the return on equity and equity multiplier : Locker Company has a debt-equity ratio of .7. Return on assets is 7.8 percent. What is the equity multiplier? What is the return on equity?
Define the workforce innovation and opportunity act : does the Workforce Innovation and Opportunity Act build upon individual, target group, and community strengths and potential strengths-based solutions
How about using a break in a switch statement : CS219 - 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
Database prevents type of problem : The data read each time through the rows may not be the same. Which feature of a database prevents this type of problem?
What affects does innovation have on competition : What affects does innovation have on competition and competitive advantage? Strategic marketing developments in informational society.
What are the total direct costs and total indirect costs : What was the flotation cost as a percentage of funds raised? What are the total indirect costs? What are the total direct costs?
Discuss the reasonably assured standard : Discuss the Reasonably Assured Standard. Discuss the Proportional and Remote Method for breakage

Reviews

Write a Review

JAVA Programming Questions & Answers

  Recursive factorial program

Write a class Array that encapsulates an array and provides bounds-checked access. Create a recursive factorial program that prompts the user for an integer N and writes out a series of equations representing the calculation of N!.

  Hunt the wumpus game

Reprot on Hunt the Wumpus Game has Source Code listing, screen captures and UML design here and also, may include Javadoc source here.

  Create a gui interface

Create GUI Interface in java programing with these function: Sort by last name and print all employees info, Sort by job title and print all employees info, Sort by weekly salary and print all employees info, search by job title and print that emp..

  Plot pois on a graph

Write a JAVA program that would get the locations of all the POIs from the file and plot them on a map.

  Write a university grading system in java

University grading system maintains number of tables to store, retrieve and manipulate student marks. Write a JAVA program that would simulate a number of cars.

  Wolves and sheep: design a game

This project is designed a game in java. you choose whether you'd like to write a wolf or a sheep agent. Then, you are assigned to either a "sheep" or a "wolf" team.

  Build a graphical user interface for displaying the image

Build a graphical user interface for displaying the image groups (= cluster) in JMJRST. Design and implement using a Swing interface.

  Determine the day of the week for new year''s day

This assignment contains a java project. Project evaluates the day of the week for New Year's Day.

  Write a java windowed application

Write a Java windowed application to do online quiz on general knowledge and the application also displays the quiz result.

  Input pairs of natural numbers

Java program to input pairs of natural numbers.

  Create classes implement java interface

Interface that contains a generic type. Create two classes that implement this interface.

  Java class, array, link list , generic class

These 14 questions covers java class, Array, link list , generic class.

Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd