Wednesday, January 11, 2012

The switch statement in JAVA

A switch statement provide you the option to test for a range of values for your variables. Switch statement helps simplifying multiple choices in a program

Syntax of Switch statement

switch(expression)

{

case value-1:Statement 1

break;

case value-2:Statement 2

break;

.........

.........

default: default Statement 3

break;

}

statement-x;

switch

The switch keyword is followed by a parenthesized integer expression. The switch statement executes the case corresponding to the value of the expression. Normally the code in a case clause ends with a break statement, which exits the switch statement and continues with the statement following the switch. If there is no corresponding case value, the default clause is executed. If no case matched and there is no default clause, execution continues after the end of the switch statement.

case

The case keyword is followed by an integer constant and a colon. This begins the statements that are executed when the switch expression has that case value.

default
If no case value matches the switch expression value, execution continues at the default clause.

break

The break statement causes execution to exit to the statement after the end of the switch. If there is no break, execution flows into the next case.

For more information visit http://www.programming-training.com/decision.html