SCJP Certification

Sun Certified Java Programmer Certification exam essentials

September 2nd, 2009

The Switch statement

Java Facts, by Daisy Williams.

The switch statement is a multiway branch statement. The general form of the switch statement is given below:

switch(expression) {
case value1://statements
break;
case value2://statements
break;
.
.
default:
//default statements
}

The expression must be of int, char, byte, or short data type and the data type of the values specified must be compatible with the expression.

Each case value must be a unique literal value. Variables are not allowed as case values.

The execution process of a switch statement is as follows:
The value of the expression is matched with each of the literal values in the case statements. If a match is found then the code sequence following that case statement is executed. If none of the values match, then the default statement is executed.

If the default statement is not given and no matching value is found, then no further action is taken.

Note: The break statement is used inside the switch statement to terminate a statement sequence. When a break statement is encountered, the execution branches to the first line of code that follows the entire switch statement block.

Share

Back Top

Responses to “The Switch statement”

Comments (0) Trackbacks (1) Leave a comment Trackback url
  1. No comments yet.
  1. What will be the output of the following program code? | SCJP Certification (,November 23, 2009)

    [...] the given program, the argument to the switch statement is an int value 2. Therefore, the program control will be transferred at the matching case label, [...]

Leave a Reply

Your email address will not be published. Required fields are marked *

*