in , ,

Java switch Statement

Java switch Statement
Java switch Statement

Java switch Statement

In this tutorial, you will learn to use the switch statement in Java to control the flow of your program’s execution

In Java, we have used the if..else..if ladder to execute a square of code among various squares. In any case, the linguistic structure of if…else…if stepping stools are excessively long.

Henceforth, we can use the switch statement as a substitute for the long if…else…if ladders. The utilization of switch articulations makes our code more clear.

The syntax of the switch statement is:

switch (variable/expression) {
case value1:
   // statements of case1
   break;

case value2:
   // statements of case2
   break;

   .. .. ...
   .. .. ...

default:
   // default statements
}

The switch statement evaluates the expression (mostly variable) and compares it with values (can be expressions) of each case label.

Now, if the value matches a certain case label, then all the statements of the matching case label are executed.

For example, if the variable/expression is equal to value2. In this case, all statements of that matching case (statements of case2) are executed.

Notice, the use of break statements in each case. The break statement is used to terminate the execution of the switch statement.

It is important because if the break is not used all the statements after the matching case are executed in sequence until the end of the switch statement.

Note: The Java switch statement only works with:

Java Primitive data types: byte, short, char, and int
Java Enumerated types
Java String Class
Java Wrapper Classes: Character, Byte, Short, and Integer.


Example 1: Java switch statement

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

        int week = 4;
        String day;

        // switch statement to check day
        switch (week) {
            case 1:
                day = "Sunday";
                break;
            case 2:
                day = "Monday";
                break;
            case 3:
                day = "Tuesday";
                break;

            // match the value of week
            case 4:
                day = "Wednesday";
                break;
            case 5:
                day = "Thursday";
                break;
            case 6:
                day = "Friday";
                break;
            case 7:
                day = "Saturday";
                break;
            default:
                day = "Invalid day";
                break;
        }
        System.out.println("The day is " + day);
    }
}

Output

The day is Wednesday

In the above example, we have used the switch statement to discover the day of seven days. Here, we have a variable week that holds an integer worth. The worth is contrasted with each case inside the switch block.

Here the value of week is 4. Hence it matches the case 4. So the statement inside case 4 is executed.


Example 2: Making Calculator using the switch statement

The program beneath takes three inputs from the user: one operator and 2 numbers. In light of the operator gave by the user, it plays out the calculation on the numbers. At that point the outcome is shown on the screen.

import java.util.Scanner;

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

        char operator;
        Double number1, number2, result;

        // create an object of Scanner class
        Scanner scanner = new Scanner(System.in);
        System.out.print("Enter operator (either +, -, * or /): ");

        // ask user to enter operator
        operator = scanner.next().charAt(0);
        System.out.print("Enter number1 and number2 respectively: ");

        // ask user to enter numbers
        number1 = scanner.nextDouble();
        number2 = scanner.nextDouble();

        switch (operator) {

            // performs addition between numbers
            case '+':
                result = number1 + number2;
                System.out.print(number1 + "+" + number2 + " = " + result);
                break;

            // performs subtraction between numbers
            case '-':
                result = number1 - number2;
                System.out.print(number1 + "-" + number2 + " = " + result);
                break;

            // performs multiplication between numbers
            case '*':
                result = number1 * number2;
                System.out.print(number1 + "*" + number2 + " = " + result);
                break;

            // performs division between numbers
            case '/':
                result = number1 / number2;
                System.out.print(number1 + "/" + number2 + " = " + result);
                break;

            default:
                System.out.println("Invalid operator!");
                break;
        }
    }
}

Output

Enter operator (either +, -, * or /): *
Enter number1 and number2 respectively: 1.4
-5.3
1.4*-5.3 = -7.419999999999999

In the above example, we have used the switch statement to create a calculator. It performs the calculation based on the operator provided by the user.


Thanks for reading! We hope you found this tutorial helpful and we would love to hear your feedback in the Comments section below. And show us what you’ve learned by sharing your photos and creative projects with us.

salman khan

Written by worldofitech

Leave a Reply

How to Blur Your Photo Background in CorelDRAW

How to Blur Your Photo Background in CorelDRAW

How to Crop a Photo in CorelDRAW

How to Crop a Photo in CorelDRAW