Search

Friday, December 14, 2018

The switch Statement

The second of Java’s selection statements is the switch. The switch provides for a multi way branch. Thus, it enables a program to select among several alternatives. Although a series of nested if statements can perform multi way tests, for many situations the switch is a more efficient approach. It works like this: the value of an expression is successively tested against a list of constants. When a match is found, the statement sequence associated with that match is executed. The general form of the switch statement is
switch
(expression) 

case constant 1: 
statement sequence 
break; 
case constant 2:
 statement sequence break;
 case constant 3:
 statement sequence 
break;
 . . . default:
 statement sequence 
}

The switch expression can be of type char,byte,short,or int.(Floating-point expressions, for example, are not allowed.)Frequently,the expression controlling the switch is simply a variable. The case constants must be literals of a type compatible with the expression . Notwo case constant sin the same switch can have identical values. The default statement sequence is executed if no case constant matches the expression. The default is optional; if it is not present, no action takes place if all matches fail. When a match is found, the statements associated with that case are executed until the break is encountered or, in the case of default or the last case, until the end of the switch is reached. The following program demonstrates the switch.

// Demonstrate the switch.
 class SwitchDemo 
public static void main(String args[])
 { 
int i;
for(i=0; i<10; i++) 
switch(i)
 { 
case 0: 
System.out.println("i is zero"); 
break;
 case 1:
 System.out.println("i is one"); 
break;
 case 2:
 System.out.println("i is two");
 break; 
case 3:
 System.out.println("i is three");
 break; 
case 4:
 System.out.println("i is four"); 
break; 
default:
 System.out.println("i is five or more");
 }
}
}
The output produced by this program is shown here:
i is zero 
i is one 
i is two 
i is three 
i is four 
i is five or more 
i is five or more 
i is five or more 
i is five or more
 i is five or more
As you can see, each time through the loop, the statements associated with the case constant that matches i are executed. All others are bypassed. When i is five or greater, no case statements match, so the default statement is executed. Technically, the break statement is optional, although most applications of the switch will use it. When encountered within the statement sequence of a case, the break statement causes program flow to exit from the entire switch statement and resume at the next statement outside the switch. However, if a break statement does not end the statement sequence associated with a case, then all the statement sat and following the matching case will be executed until a break(or the end of the switch) is encountered. For example, study the following program carefully. Before looking at the output, can you figure out what it will display on the screen?

// Demonstrate the switch without break statements. 
class NoBreak 
{
 public static void main(String args[])
 {
 int i;
for(i=0; i<=5; i++) 
{ switch(i) 
case 0:
 System.out.println("i is less than one"); 
case 1:
 System.out.println("i is less than two");
 case 2: 
System.out.println("i is less than three");
 case 3: 
System.out.println("i is less than four"); 
case 4: 
System.out.println("i is less than five");
System.out.println();
}
}
}

This program displays the following output:

i is less than one 
i is less than two 
i is less than three
 i is less than four
 i is less than five
i is less than two 
i is less than three 
i is less than four
 i is less than five
i is less than three 
i is less than four
 i is less than five
i is less than four
 i is less than five
i is less than five
As this program illustrates, execution will continue into the next case if no break statement is present. You can have empty cases, as shown in this example:

switch(i)
 {
 case 1:
 case 2:
 case 3:
 System.out.println("i is 1, 2 or 3");
 break; 
 case 4: 
 System.out.println("i is 4"); 
 break;
 }
In this fragment, if i has the value 1, 2, or 3, the first print ln( )statement executes. If it is 4, the second println( )statement executes. The “stacking” of cases, as shown in this example, is common when several cases share common code.

0 comments:

Post a Comment