Index

chapter1

chapter2page1

chapter2page2

chapter2page3

chapter2page4

chapter2page5

chapter2page6

chapter2page7

chapter2page8

chapter2page9

chapter2page10

chapter2page11

chapter2page12

chapter2page13

chapter2page14

chapter2page15

chapter2page16

chapter2page17

chapter2page18

chapter2page19

chapter2page20

chapter2page21

chapter2page22

chapter2page23

chapter2page24

chapter2page25

chapter2page26

chapter2page27

chapter2page28

chapter2page29

chapter2page31

chapter2page32

chapter2page33

chapter2page34

chapter3

link one

link one

link one

link one

link one

link one

2.7.5 -Switch Statement

The modern tool for branching according multiple conditions is switch statement. This statement consists from a switch statement followed with a switch block, as it may be seen below:

switch (switch statement) {
}

The switch statement may be any expression reducing to a definite value. This value type may be char, byte, short, int, Character, Byte, Short, Integer, String, or an enum type. With Java SE 7, switch statement is enhanced to include String data type. If the switch expression may not result to the types stated above, a compile-time error will be generated.

As an example, if shirtColor is a variable of type String, the following switch block may be organised:

switch (shirtColor) {
}

Inside the switch block, there is as many switch labels as it may needed. A switch label is a construct consisting a case keyword followed by a colon and the value which the switch expression meant to match. A general scheme is as follows:

case value:
statements ...

The statements in the case construct may be taken into block parenthesis but generally, there might no need to use block parethesis owing to the clearer syntax of the switch statement.

A switch block may contain only one default construct which has no value to match and meant to be executed when no match with all the case constructs occurred.

Now, be prepared to the shock: All the statements in a switch block is executed sequentially from top to down, nothing is executed unless a matching case is encountered. Once a matching case is found, all the statements succeeding are executed regardless of the case constructs. An example is below:

package preliminary;

public class SwitchBlockRaw {

public static void main(String[] args){
String shirtColor = "Red";
switch (shirtColor) {
case "Blue" :
System.out.println("The Shirt Color is Blue");
case "Red" :
System.out.println("The Shirt Color is Red");
default :
System.out.println("The Shirt Is Not Colored !");
 } // end switch

 } // end main

} //end class SwitchBlockRaw


The result of this program :

The Shirt Color is Red
The Shirt Is Not Colored

OOPS!, the statements within the matching case is executed. This was the intention of using the switch block. But the statements of the defalut label is also executed, this was certainly not what we are waiting for!!! If the switch block will produce useful results, some mechanism preventing the execution of the statements included in the succeeding case labels should beprovided.

The mechanism sought is breakstatement. This statement terminates the executing process and transfer the control to the next statement. The break statement is placed as the last statement in case construct. In that way, the case constructs other the maching one is prevented from executing. The example below, i the right formulation of the preceding example:

package preliminary;

public class SwitchOne {

public static void main(String[] args){
String shirtColor = "Blue";
switch (shirtColor) {
case "Blue" :
System.out.println("The Shirt Color is Blue");
break;
case "Red" :
System.out.println("The Shirt Color is Red");
break;
default :
System.out.println("The Shirt Is Not Colored !");
 } // end switch

 } // end main

} //end class SwitchOne

The result of this program :

Blue Shirt !

This is the expected result.

The case constructs may be juxtaposed to drive the same set of statements. The example below illustrates this situation:

package preliminary;

public class SwitchTwo {

public static void main(String[] args){
String shirtColor = "Blue";
switch (shirtColor) {
case "Blue" : case "Red" :
 System.out.println("The Shirt  is Colored !");
break;
default :
System.out.println("The Shirt Is Not Colored !");
 } // end switch

 } // end main

} //end class SwitchTwo

The result of this program :

The Shirt  is Colored !

In java, switch statements may ne nested. That is, we may organize a new switch statement within a case of a switch statement. We may also label the external switch statement. In Java, labels are simple identifiers followed by a colon. We may then be able to issue break statement within the case statement of the inner switch statement to the outer switch statement as it is shown below:


externalswitcher  : switch (expression) {
case   value:
switch (expression){
case value :
....

break; // This is a Normal break !
break externalswitcher; //This Breaks the externalswitcher case
....
 } // end switch inner
....
 } // end externalswitcher

Labeled nested switch statements are very rare if not unused at all. We are covering them only for the sake of completion.

<< Index

Polyglott HTML5(XHTML5 compliant HTML5 code)