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

Gps and gprs fleet management system, GPS and GPRS fleet management system ...

GPS and GPRS fleet management system Project Description: I want to develop fleet management system Skills required: HTML, PHP, Java, Website Design

Describe even higer dimensions in java, Describe even higer dimensions in j...

Describe even higer dimensions in java? You don't have to stop along with two dimensional arrays. Java allows arrays of three, four or more dimensions. Therefore chances are pr

What is jms consumer, JMS consumer is a JMS client that receives messages.

JMS consumer is a JMS client that receives messages.

Difference between the file and randomaccessfile classes, The File class en...

The File class encapsulates the files and directories of the local file system. The RandomAccessFile class gives the methods required to directly access data contained in any part

Online doctor, can you explain me the er diagram for the online doctor syst...

can you explain me the er diagram for the online doctor system

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

Java developer with spring framework prerequisite, Java developer with spri...

Java developer with spring framework prerequisite from anywhere Let me know that you have proven experience in java with spring frame work. Send some sample data and provide inf

Design a website in java using xml, The design of the website will be left ...

The design of the website will be left to the individual student. The website must be written only in xml, displayed in xhtml using transforms and xsl for styling elements. Javascr

What is an objects lock, What is an object's lock and which objects have lo...

What is an object's lock and which objects have locks? An object's lock is a mechanism that is used by multiple threads to get synchronized access to the object. A thread may i

Why we avoiding short circuits, Why we Avoiding Short Circuits ? If you...

Why we Avoiding Short Circuits ? If you need all of your boolean expressions evaluated regardless of the truth value of each, then you can use & and | instead of && and ||. The

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