Share on Google+Share on Google+

Java Switch Statement

In java, switch is one of the control statement which turns the normal flow control of the program as per conditions.

Java Switch Statement


In java, switch is one of the control statement which turns the normal flow control of the program as per conditions. It's like if-else statement but it can reduce the number of if statements. When number of cases are limited then switch statement can be used. It checks your choice and jump on that case label if the case exists otherwise the statement with default label is executed. It works with the byte, short, char, int primitive data types and enumerated types. 


Switch case is followed by a parenthesized integer expression, that further follows cases, all enclosed in braces. The switch statement executes the cases regarding the values of the expression. Code in a case clause must ends with a break statement, exiting with the switch statement and continues with the statement following the switch. default clause is executed if no corresponding case value exist. Execution continues after the end of the switch statement if no case is matched and no default clause exist.

The case keyword is followed by an integer constant and a colon. Statements in the case are executed when the switch expression matches with the case value.

If the switch case values does not matches with any case value, control goes to the default clause. It is equivalent to the "else" with the switch statement. default comes after the last case that typically does not followed by break because execution just continues out the bottom of switch if this is the last clause.

The break statement results in, passes the control to the statement after the end of the switch. Execution flows thru into the next case if no break exists. Passing control directly to the next case is almost always an error.

To know more about the switch statement click on:  




Posted on: November 17, 2009 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Share this Tutorial Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Discuss: Java Switch Statement  

Post your Comment

Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
December 19, 2011