Switch in Java

Java Switch Statement

Switch in Java is like if-else-if ladder statement. It executes one block out of the many blocks mentioned for multiple conditions. It assesses the equality of variable of type int, short, bytes, long, enum, String and some wrapper classes against multiple values.

Syntax:

switch(expression){
case value1:
    //code 
    break;//optional
case value2:
    //code 
    break;//optional
...
case valueN:
    //code 
    break;//optional
default:
    //code that executes when none of the cases match the evaluated expression
}

Note:

    • Switch allows N number of cases in it whose values must be of switch expression type only.
    • Case values must be literals or constants since variables are not allowed and should be unique. If they are not unique, then a compile-time error will occur.
    • Each case statement of switch has a break statement at the end of it which is optional. Break statement allows the switch to execute only 1 statement. If break is not there between two cases out of which 1st matches, then the 2nd case statements will also run. Break statement brings the control out of the switch to the immediate next statement after it.
    • Default statement can be placed in between the cases instead of in the end. 

Flowchart:

switch statement

 

Example of Switch Statement in Java:

//Java program with switch statement
public class Switch {
    public static void main(String[] args){
        //defining a variable "lucky_number" with value 1
        int lucky_number=1;
        switch(lucky_number){
            case 1: 
            //executes if lucky_number is 1
            System.out.println("Congratulations!! You won 1st prize.");
            break;
            case 2:
            //executes if lucky_number is 2
            System.out.println("Congratulations!! You won 2nd prize.");
            break;
            default:
            //executes if lucky_number is neither 1 nor 2
            System.out.println("Sorry!! You lost.");
        }
    }
}

In the program provided above, a variable “lucky_number” has been created and initialized to 1. It is then used in switch expression and matched with the cases. Two cases and a default label has been used.

Output:

Congratulations!! You won 1st prize.

 

 

Switch Case Program in Java Using Scanner

//Java program that uses a Switch and a Scanner objet to take input
import java.util.Scanner;
public class SwitchScanner {
    public static void main(String[] args){
        //creating a Scanner object sc
        Scanner sc=new Scanner(System.in);

        //declaring a variable names number
        int number;
        System.out.print("Enter any number: ");

        //value entered by the user stored in variable "number"
        number=sc.nextInt();

        //switch expression to check if "number" is even or odd
        switch(number%2){
            case 0:
            //executes of "number" is even
            System.out.println(number+" is even.");
            break;
            case 1:
            //executes if "number" is odd
            System.out.println(number+" is odd.");
            break;
        }
        sc.close();

    }
}

In the program provided above, Scanner class has been imported which is used to take input from the user. A variable has been created called “number” and is assigned the value entered by the user by changing its type to int. Input Stream in Java returns everything as a String object. So, Scanner class provides various method to change the data type from String to the one that you need which is int in our case. Switch expression uses a modulus operator with “number” and a numeric literal 2. The evaulated expression is then matched with the cases.

Output:

Enter any number: 25
25 is odd.

 

Share this post

One thought on “Switch in Java

Leave a Reply

Your email address will not be published. Required fields are marked *