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

Overview of struts?, Struts is a framework with set of co operating servlet...

Struts is a framework with set of co operating servlets, classes and JSP tags that create a reusable MVC 2 design.     1. Client (Browser) : A request from the cli

Method to define the packages in java programme, Q. Write the method to def...

Q. Write the method to define the packages in java programme. Explain. Ans. Package: When we work on a project we have to break our programme in several classes. To organize

Change to palindrome, A palindrome is a string that reads the same from bot...

A palindrome is a string that reads the same from both the ends. Given a string S convert it to a palindrome by doing character replacement. Your task is to convert S to palindrome

Program to create 5 objects and get them interact together, In this assignm...

In this assignment, you will create 5 objects and get them to interact together. You will create theatres for which patrons will buy tickets from a box office to watch movies.

Create a generic class with a type parameter, 1. The purpose of this proble...

1. The purpose of this problem is to practice using generics.  Create a generic class with a type parameter that simulates drawing an item at random out of a box. For exampl

What is synchronous messaging, Synchronous messaging involves a client that...

Synchronous messaging involves a client that waits for the server to respond to a message. So if one end is down the whole communication will fail.

What are the non-final functions in java object class?, The non-final funct...

The non-final functions are clone (), finalize (), toString (), equals () , hashCode () and. The other methods like wait (), getClass (), notifyAll (), notify () etc are final

Boardcolourling, write a program to find the minimum no of colors to fill a...

write a program to find the minimum no of colors to fill a board where some of the elements of different color are already placed.

2D arrays, write an application that stores at least five different departm...

write an application that stores at least five different department and supervisor names in a two dimensional array

Explain multiple initializers and incrementers, Explain Multiple Initialize...

Explain Multiple Initializers and Incrementers ? Sometimes it's essential to initialize several variables before starting a for loop. Similarly you may need to increment more t

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