Register Now

Login

Lost Password

Enter your email to reset your password.

BY Author

The switch statement

In Java programming, the switch statement is used to make a specific selection from multiple cases.

Syntax

switch(expression)
{
    case constant_value_1 :
                             statements to be executed;
                             break;
    case constant_value_2 :
                             statements to be executed;
                             break;
    case constant_value_3 :
                             statements to be executed;
             |               break;
             |
             |

    case constant_value_N :
                             statements to be executed;
                             break;
    case default :
                             statements to be executed;
}

In a switch statement, the expression inside switch’s left & right parenthesis is executed first.
If any case matches the expression value, program control jumps directly to that case and statements of that case are executed.
If expression value does not match with any case, then program control jumps directly to the default case and statements of default case are executed.

Note

Along with integer & characters, strings can also be used as constants with cases.

Flowchart

Java Programming Language switch Statement Flowchart

Switch statement working demo

Java Programming Language switch Statement Working

Example No. 1

public class SwitchDemo1 
{
    public static void main(String args[])
    {
        int num = 3;
        switch(num)
        {
            case 1:
                   System.out.println("Inside Case 1");
            case 2:
                   System.out.println("Inside Case 2");
            case 3:
                   System.out.println("Inside Case 3");
            case 4:
                   System.out.println("Inside Case 4");
            case 5:
                   System.out.println("Inside Case 5");
            default:
                   System.out.println("Inside default case");
        }
        System.out.println("After Switch Statement");
    }
}

Output

Inside Case 3
Inside Case 4
Inside Case 5
Inside default case
After Switch Statement

In the above program, the value assigned to variable num is 3, so it matches case 3 and program control jumps directly to case 3 skipping unmatched case 1 & case 2.

But when the program enters case 3 and executes it, all the cases below it including default case are also executed because we have not written break statement inside all the cases.

If we don’t want other cases to get execute after selected case, we should use the break statement in each case at the end, as shown below:

Example No. 2

class SwitchDemo2 
{
    public static void main(String args[])
    {
        int num = 3;
        switch(num)
        {
            case 1:
                   System.out.println("Inside Case 1");
                   break;
            case 2:
                   System.out.println("Inside Case 2");
                   break;
            case 3:
                   System.out.println("Inside Case 3");
                   break;
            case 4:
                   System.out.println("Inside Case 4");
                   break;
            case 5:
                   System.out.println("Inside Case 5");
                   break;
            default:
                   System.out.println("Inside default case");
        }
        System.out.println("After Switch Statement");
    }
}

Output

Inside Case 3
After Switch Statement

Example No. 3

class SwitchDemo3 
{
    public static void main(String args[])
    {
        char ch = 'C';
		
        switch(ch)
        {
            case 'A':
                     System.out.println("Inside Case A");
                     break;
            case 'B':
                     System.out.println("Inside Case B");
                     break;
            case 'C':
                     System.out.println("Inside Case C");
                     break;
            case 'D':
                     System.out.println("Inside Case D");
                     break;
            case 'E':
                     System.out.println("Inside Case E");
                     break;
            default:
                     System.out.println("Inside default case");
        }
        System.out.println("After Switch Statement");
    }
}

Output

Inside Case C
After Switch Statement

Note

In switch statement, default case can be used in any order.

 

Leave a reply