The Java switch statement executes one statement from multiple conditions. It is like if-else-if ladder statement.

  • Important points

    • For versions of Java prior to JDK 7, expression must resolve to type byte, short, int, char, or an enumeration. Beginning with JDK 7, expression can also be of type String. Each value specified in the case statements must be a unique constant expression (such as a literal value). Duplicate case values are not allowed. The type of each value must be compatible with the type of expression.

    • The switch statement works like this: The value of the expression is compared with each of the values in the case statements. If a match is found, the code sequence following that case statement is executed. If none of the constants matches the value of the expression, then the default statement is executed. However, the default statement is optional. If no case matches and no default is present, then no further action is taken.

      555253408.jpg
    • The break statement is used inside the switch to terminate a statement sequence. When a break statement is encountered, execution branches to the first line of code that follows the entire switch statement. This has the effect of " jumping out " of the switch.


    Syntax:

    switch(expression){    
    case value1:    
     //code to be executed;    
     break;  //optional  
    case value2:    
     //code to be executed;    
     break;  //optional  
    ......    
        
    default:     
     code to be executed if all cases are not matched;    
    }    


    Simple Example

    //example of switch statement 
    class TestingSwitch
    {
    public static void main(String[] args) {
     int n=3;
     switch(n)
     {
        case 0:
        System.out.println("Zero");
        break;
    
        case 1:
        System.out.println("One");
        break;
        
        default :
        System.out.println("Default case"); 
        break;
     }   
    }
    }
    

    Output:
    Default case
  • fall-through

    The java switch statement is fall-through. It means it executes all statement after first match if break statement is not used with switch cases.

    Example)
    drag_handle
     public class SwitchExample2 {  
    public static void main(String[] args) {  
        int number=20;  
        switch(number){  
        case 10: System.out.println("10");  
        case 20: System.out.println("20");  
        case 30: System.out.println("30");  
        default:System.out.println("Not in 10, 20 or 30");  
        }  
    }  
    }  
    Output:
    20
    30
    Not in 10, 20 or 30
  • Nested switch Statements

    You can use a switch as part of the statement sequence of an outer switch. This is called a nested switch. Since a switch statement defines its own block, no conflicts arise between the case constants in the inner switch and those in the outer switch.

    Example)
    drag_handle
     
    //example of inner switch statement 
    class TestingSwitch
    {
    public static void main(String[] args) {
     int n=0;
     int n1=0;
     switch(n)
     {
        case 0:
        System.out.println("Case Zero");
            //creating inner switch block
             switch(n1)
            {
            case 0:
            System.out.println("Zero Zero block");
            break;
    
            default:
            System.out.println("Inner default block");
            }
            //closing inner switch block
        break;
    
    
        case 1:
        System.out.println("One");
            //creating inner switch block for case 1
            switch(n1)
            {
            case 0:
            System.out.println("Zero Zero block");
            break;
    
            default:
            System.out.println("Inner default block");
            }
            //closing inner switch block
        break;
        
        default :
        System.out.println("Default case"); 
        break;
     }   
    }
    }
    
    Output:
    Case Zero
    Zero Zero block

Simple example of switch case:
drag_handle
//example of switch statement 
class TestingSwitch
{
public static void main(String[] args) {
 int n=3;
 switch(n)
 {
    case 0:
    System.out.println("Zero");
    break;

    case 1:
    System.out.println("One");
    break;

    case 2:
    System.out.println("Two");
    break;

    case 3:
    System.out.println("Three");
    break;

    default :
    System.out.println("Default case"); 
 }   
}
}

Output:

Three


Example of switch statement(String in argument ) .
drag_handle
//example of switch statement(which takes String ) 
class TestingSwitch
{
public static void main(String[] args) {
 String day="thursday";
 switch(day)
 {
    case "mondey":
    System.out.println("Today is Mondey");
    break;

    case "tuesday":
    System.out.println("Today is Tuesday");
    break;

    case "wednesday":
    System.out.println("Today is Wednesday");
    break;

    case "thursday":
    System.out.println("Today is Thursday");
    break;

    default :
    System.out.println("Default case"); 
 }   
}
}

Output:

Today is Thursday

What next?

The next topic is For Loops in java

Share this page