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

Multitreading, what is multithreading in Java? Explain with example.

what is multithreading in Java? Explain with example.

Application of server clustering? , An application server cluster has of a ...

An application server cluster has of a number of application servers loosely coupled on a network. The server group or server cluster is usually distributed over a number of nodes

Implement a java class, Your first task is to implement the Movie class to ...

Your first task is to implement the Movie class to store all the relevant information on a single DVD Movie, including its catalog number, title, year of release, rating, rental pe

Why class can''t extend more than one class, Why is an Interface be able to...

Why is an Interface be able to extend more than one Interface but a Class can't extend more than one Class? Basically Java doesn't permit multiple inheritances, so a Class is r

Describe what is bytecode, Describe what is bytecode? It is an inst...

Describe what is bytecode? It is an instruction set. It extends with class. 'javac' compiler translates the .java file into .class. JVM interprets bytecode.

Benefits of spring framework, Benefits of Spring framework : Spring can...

Benefits of Spring framework : Spring can effectively organize your middle tier objects as given in the diagram above, whether or not you select to use EJB. Applications create

I need a dcs source code and jar, I need a DCS (Source code and JAR) to pro...

I need a DCS (Source code and JAR) to process packets from a cheap chinese GPS TK110, the unit is already communicating with the server but is not parsing. So I need someone with e

Need cocos2d-x developer - puzzle game, cocos2d-x puzzle game for kids P...

cocos2d-x puzzle game for kids Project Description: I am seeking a skilled, reliable, and not very expensive cocos2d-x developer (with experience with iOS, Android, and prefe

Get info from website using javascript, Get info from website using javascr...

Get info from website using javascript Project Description: I need to extract the text from all the available (red, blue) seats of all the events on this site without getting

Develop a farm production system, Develop a Farm Production System Proje...

Develop a Farm Production System Project Description: I want software that will allow me to input/record the each day production of our farm and allow me to view the data bac

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