Question

In: Computer Science

Does break have to be used in each section of statements that follow a case in...

Does break have to be used in each section of statements that follow a case in a switch statement? Detail your answer. *In Java*

Solutions

Expert Solution

It depends on the requirement.

Consider the below code:

int i=1;
       switch(i) {
       case 1:
           System.out.println("First");
       case 2:
           System.out.println("Second");
       case 3:
           System.out.println("Third");
       case 4:
           System.out.println("Fourth");
       default:
           System.out.println("Default");
       }

Output:

=====

First

Second

Third

Fourth

Default

In the above code, if we pass i=1, we will get output of all the cases including default as there is no break statement. This can be used in situations when we need all the case statements greater than a particular value. If we want to execute all the statements from case 3 on wards, then we can pass 3 as the value for i.

When we want execute only one case statement and we don't want other case statements to be executed then we can use break statement as shown below:

int i=1;
       switch(i) {
       case 1:
           System.out.println("First");
           break;
       case 2:
           System.out.println("Second");
           break;
       case 3:
           System.out.println("Third");
           break;
       case 4:
           System.out.println("Fourth");
           break;
       default:
           System.out.println("Default");
       }

Output:

======

First

In this code we have included break statements for all the cases. So when we pass 1 as the value of i then we get "First" in the output.


Related Solutions

For each of the statements in Section A, determine whether it is true or false, then...
For each of the statements in Section A, determine whether it is true or false, then explain in a few sentences why that is the answer. Note: no marks will be given for answers that do not include an explanation. 1. If money stops being a reliable store of value, then it ceases to be useful as a medium of exchange. 2. The gold standard is an improvement over actual gold coinage because it allows the government/central bank to control...
he case scenario provided will be used to answer the discussion questions that follow. Case Scenario...
he case scenario provided will be used to answer the discussion questions that follow. Case Scenario Mr. C., a 32-year-old single man, is seeking information at the outpatient center regarding possible bariatric surgery for his obesity. He reports that he has always been heavy, even as a small child, but he has gained about 100 pounds in the last 2–3 years. Previous medical evaluations have not indicated any metabolic diseases, but he says he has sleep apnea and high blood...
The 2014 and 2013 income statements and balance sheets (asset section only) for Target Corporation follow,...
The 2014 and 2013 income statements and balance sheets (asset section only) for Target Corporation follow, along with its footnote describing Target’s accounting for property and equip-ment. Target’s cash flow statement for fiscal 2014 reported capital expenditures of $1,786 million and disposal proceeds for property and equipment of $95 million. No gain or loss was reported on property and equipment disposals. In addition, Target acquired property and equipment through non-cash acquisitions not reported on the statement of cash flows. Consolidated...
CASE 3 (25 points) Income statements and balance sheets for Melia Corporation follow. Income Statements for...
CASE 3 (25 points) Income statements and balance sheets for Melia Corporation follow. Income Statements for Years 2015 and 2016 2015 2016 Net sales $438,000 $575,000 - Cost of goods sold -285,000 -380,000 Gross profit 153,000 195,000 -Administrative expenses -45,000 -65,000 -Marketing expenses -32,000 -39,000 -Research and development -20,000 -27,000 -Depreciation -14,000 -17,000 EBIT 42,000 47,000 -Interest expense -12,000 -20,000 income before taxes 30,000 27,000 -Income before taxes -9,000 -8,000 Net income $21,000 $19,000 Balance Sheets for Years Ended 2014,...
A) Is it important to have a break after every case in the switch statement?) b)...
A) Is it important to have a break after every case in the switch statement?) b) Explain lain your answer by writing an example of a switch statement and showing the effect of including and excluding the break.                                                                                                                                                               10 points A) String Name = KB.NextLine();       Write a java statement to convert Name to upper case. B) char Letter =KB.next().charAt(0);           Write a java statement to convert Letter to lower case.                                                                                                                                                        5 points c) Write a while loop which...
A. True or false (9 marks total) For each of the statements in Section A, determine...
A. True or false (9 marks total) For each of the statements in Section A, determine whether it is true or false, then explain in a few sentences why that is the answer. Note: no marks will be given for answers that do not include an explanation. 1. In practice, the Bank of Canada sets a target for real money balances and then the interest rate automatically adjusts to ensure this target is met. 2. In the Mundell-Fleming model, in...
Are the following statements true or false? Explain in each case.
Are the following statements true or false? Explain in each case.a. “Two countries can achieve gains from trade even if one of the countries has an absolute advantage in the production of all goods.”b. “Certain very talented people have a comparative advantage in everything they do.”c. “If a certain trade is good for one person, it can’t be good for the other one.”
Consider the follow management techniques used by management accountants. Break-even point (Ch. 22) Budget (Ch. 24)...
Consider the follow management techniques used by management accountants. Break-even point (Ch. 22) Budget (Ch. 24) Balanced scorecard (Ch. 26) Capital budgeting (Ch. 27) Pick two of the techniques and explain what they are and demonstrate how they might be used in a personal or professional situation.
Identify each statement in all questions of this section as T (for true statements) or F...
Identify each statement in all questions of this section as T (for true statements) or F (for false statements). Each statement is worth one mark. Eukaryotic mRNAs contain a poly(A) tail at the 3'-end, but the template DNA encoding the mRNA does not have poly(T)s. Identify the statements as true or false. The poly A tail is formed by RNA polymerase The poly A tail is formed by DNA polymerase II The poly A tail is formed by poly A...
Find the matrix A representing the follow transformations T. In each case, check that Av =...
Find the matrix A representing the follow transformations T. In each case, check that Av = T(v) T(T(x,y,z)) where T(x,y,z)=(x-3y+4z, 6x-2z, 8x-y-4z)
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT