Switch Statement in Java.

In java programming, switch statement executes single statement from multiple conditions given. The switch expression is evaluated once and value of the expression is compared with the value of the each case in switch. If the match is found then the associated code block is executed.

Important Rules for Switch Case.

The value of the case should be constant or literal and variables are not allowed.

The duplicate case values are not allowed.

The value of the case should be of the same datatype.

We use break statement inside each case of the switch statement to terminate the sequence.

However, break statement is optional, if we donot use it will continue to the next case.

Default statement is also optional.

Syntax

switch(expression){
     case value 1 :
         //Statement
     break;
     
     case value 2 :
         //Statement
     break;

    case value n :
         //Statement 
    break;

   default :
        // default statement
}

Example 1

You can find the code here

package com.qacaffe.examples.ControlStatements;

public class SwitchExe1 {
    public static void main(String[] args) {

        char c = 'C';
        System.out.println("Example of switch statement in java.");

        switch (c) {
            case 'A':
                System.out.println("A is printed.");
                break;
            case 'B':
                System.out.println("B is printed.");
                break;
            case 'C':
                System.out.println("C is printed.");
                break;
            case 'D':
                System.out.println("D is printed.");
                break;
            default:
                System.out.println("Invalid Input !!! ");
        }
    }
}

/*
Output:
Example of switch statement in java.
C is printed.
 */

Example 2

You can find the code here

package com.qacaffe.examples.ControlStatements;

import java.util.Scanner;

public class SwitchExe2 {
    public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);
        System.out.print("Enter day : ");
        int day = scanner.nextInt();

        switch (day) {
            case 1:
                System.out.println("Today is Sunday.");
                break;
            case 2:
                System.out.println("Today is Monday.");
                break;
            case 3:
                System.out.println("Today is Tuesday.");
                break;
            case 4:
                System.out.println("Today is Wednesday.");
                break;
            case 5:
                System.out.println("Today is Thursday.");
                break;
            case 6:
                System.out.println("Today is Friday.");
                break;
            case 7:
                System.out.println("Today is Saturday.");
                break;
            default:
                System.out.println("Invalid Input !!! ");
        }
    }
}

/*
Output :
Enter day : 5
Today is Thursday.

 */

Example 3

You can find the code here

package com.qacaffe.examples.ControlStatements;

public class SwitchExe3 {
    public static void main(String[] args) {

        String programName = "Computer Software Testing";
        int block = 2;

        switch (block) {
            case 1:
                System.out.println("You have selected algorithms and operating systems.");
                break;
            case 2:
                switch (programName) {
                    case "Computer Software Testing":
                        System.out.println("You have selected linux and introduction to object oriented programming.");
                        break;

                    case "Mobile Application Development":
                        System.out.println("You have selected kotlin and ios development.");
                        break;

                    case "Cyber Security":
                        System.out.println("You have selected unix and penetration testing.");
                        break;

                    default:
                        System.out.println("You have selected to learn Selenium and Rest Assured APIs.");
                }
        }
    }
}

/*
Output:
You have selected linux and introduction to object oriented programming.
 */

Rutu Shah