Explain switch statement in java language, JAVA Programming

Assignment Help:

Explain switch statement in java language?

Switch statements are shorthands for a certain type of if statement. It is not common to see a stack of if statements all related to the similar quantity like this:

if (x == 0) doSomething0();
else if (x == 1) doSomething1();
else if (x == 2) doSomething2();
else if (x == 3) doSomething3();
else if (x == 4) doSomething4();
else doSomethingElse();

Java has a shorthand for these kinds of multiple if statements, the switch-case statement. Here's how you'd write the above using a switch-case:

switch (x) {
  case 0: 
    doSomething0();
    break;
  case 1: 
    doSomething1();
    break;
  case 2: 
    doSomething2();
    break;
  case 3: 
    doSomething3();
    break;
  case 4: 
    doSomething4();
    break;
  default: 
    doSomethingElse();
}

In this fragment x must be a variable or expression in which can be cast to an int without loss of precision. This means the variable must be or the expression must return an int, byte, short or char. x is compared along with the value of each the case statements in succession until one matches. This fragment compares x to literals, other than these too could be variables or expressions as long as the variable or result of the expression is an byte, int, short or char. The default action is triggered if no cases are matched.

Once a match is found, all following statements are executed until the end of the switch block is reached or you break out of the block. This could trigger decidedly unexpected behavior. Thus it is general to involve the break statement at the end of each case block. It's excellent programming practice to put a break after each one unless you explicitly want all following statements to be executed.

It's significant to remember that the switch statement doesn't end while one case is matched and its action performs. A program then executes all statements in which follow in which switch block until specifically told to break.


Related Discussions:- Explain switch statement in java language

Calculates whether a plane successfully lands or not, Write a program calle...

Write a program called LandThePlane that calculates whether a plane successfully lands or not.   The program begins by prompting (asking) the user for the following details, in th

Program to brute force search, Ask questionWrite a program BruteForceSearch...

Ask questionWrite a program BruteForceSearch that uses the brute-force approach given above and compare its running time on your computer with that of Binary Search for largeW.txt

Explain try and catch exception in java, Explain try and catch exception in...

Explain try and catch exception in java? Why use exceptions instead of return values? 1. Forces error checking 2. Cleans up your code through separating the normal case from

Explain super class and sub class in java, 1. In Java (and OOP), Inheritanc...

1. In Java (and OOP), Inheritance includes the concept of super class that is parent class and sub class that is derived class. Explain What is a super class in Java? What is a sub

Write a short Java application that stores words in an Array, Write a short...

Write a short Java application that stores words in an Array or ArrayList. You get to pick the number of words to store. Generate a random number between 0 (inclusive) and the leng

How to draw polygons in java, How to draw Polygons in java? In Java rec...

How to draw Polygons in java? In Java rectangles are defined through the position of their upper left hand corner, their height, and their width. Therefore it is implicitly sup

Introduction, the multiple of two number in java

the multiple of two number in java

Please answer this, Assignment Help >> JAVA Programming Learning Outcomes:...

Assignment Help >> JAVA Programming Learning Outcomes: 1. Use different program control statements in a programming language 2. Demonstrate the use of arrays with a programming

How to use dispatchaction, To use the DispatchAction, follow these steps: ...

To use the DispatchAction, follow these steps: ? Make a class that extends DispatchAction (instead of Action) ? In a new class, add a method for each function you need to per

Write Your Message!

Captcha
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