Home > Java > Switch Statement > Introduction
Introduction

Introduction to Switch Statement

The switch is sometimes classified as a selection statement. The switch statement selects from among pieces of code based on the value of an integral expression.


Integral-selector is an expression that produces an integral value. The switch compares the result of integral-selector to each integral-value. If it finds a match, the corresponding statement (simple or compound) executes. If no match occurs, the default statement executes. You will notice in the above definition that each case ends with a break, which causes execution to jump to the end of the switch body. This is the conventional way to build a switch statement, but the break is optional.


If it is missing, the code for the following case statements execute until a break is encountered. Although you don’t usually want this kind of behavior, it can be useful to an experienced programmer. Note the last statement, following the default, doesn’t have a break because the execution just falls through to where the break would have taken it anyway. You could put a break at the end of the default statement with no harm if you considered it important for style’s sake. The switch statement is a clean way to implement multi-way selection (i.e., selecting from among a number of different execution paths), but it requires a selector that evaluates to an integral value such as int or char. If you want to use, for example, a string or a floating-point number as a selector, it won’t work in a switch statement. For non-integral types, you must use a series of if statements.


Syntax



switch (integral-selector) {
case integral-value1 : statement;  break;
case integral-value2 : statement;  break;
case integral-value3 : statement;  break;
case integral-value4 : statement;  break;
case integral-value5 : statement;  break;
// ...
default: statement;
}


Example


1.  /**
2.  * @(#)SwitchStatementDemo.java
3.  *
4.  *
5.  * @author
6.  * @version 1.00 2009/11/6
7.  */
8.
9.  public class SwitchStatementDemo {
10.        
11.    /**
12.     * Creates a new instance of <code>SwitchStatementDemo</code>.
13.     */
14.    public SwitchStatementDemo() {
15.    }
16.    
17.    /**
18.     * @param args the command line arguments
19.     */
20.    public static void main(String[] args) {
21.        // TODO code application logic here
22.        
23.        int day = 5;
24.        
25.        switch (day) {
26.            
27.            case 1:
28.                System.out.println("Monday");
29.                break;
30.            case 2:
31.                System.out.println("Tuesday");
32.                break;
33.            case 3:
34.               System.out.println("Wednesday");
35.                break;
36.            case 4:
37.               System.out.println("Thursday");
38.                break;
39.            case 5:
40.                System.out.println("Friday");
41.                break;
42.            case 6:
43.                System.out.println("Saturdary");
44.                break;
45.            case 7:
46.                System.out.println("Sunday");
47.                break;
48.                
49.            default:
50.                System.out.println("Please enter correct number.");
51.                
52.            
53.        }
54.    }
55. }


Output


Friday

Leave a Reply

Fields with * are mandatory.
* Name:
Website:
* Comments:
 
Tags allowed: <strong> <i> <u>
* Code:
 
 
Submit   Cancel