Java Program to Implement switch statement on strings

Java Program to Implement switch statement on strings

What are Switch Case Statements?

In this Article, we will write a Program to Implement switch statement on strings.
In Java, a switch statement is used to perform different actions based on different conditions. The switch statement compares the value of a variable (or an expression) to a list of possible cases, and when a match is found, the code associated with that case is executed. The switch statement can be used as an alternative to a series of if-else statements.

Switch statements:

A switch case statement in Java is a control flow statement that allows you to choose between multiple different actions based on the value of an expression. The expression is evaluated and the corresponding case that matches the value of the expression is executed.

Basic syntax :

switch (expression) {
    case value1:
        // code to be executed if expression == value1
        break;
    case value2:
        // code to be executed if expression == value2
        break;
    ...
    default:
        // code to be executed if expression does not match any of the cases
}

The expression is evaluated and compared to the case values. If a match is found, the code block associated with that case is executed. If no match is found, the code block associated with the default case is executed (if one is provided).

It’s important to use the break statement after each case block to exit the switch statement and prevent the execution of any following cases. If you don’t use the break statement and a match is found, all of the following cases will be executed as well.

Java Program to Implement switch statement on strings:

Run
// Importing all the required packages
import java.util.Scanner;
import java.util.*;

public class Main{
    public static void main(String[] args){
        // Scanner class for taking input
        Scanner scn = new Scanner(System.in);
        System.out.print("Enter a color name: ");
        String color = scn.nextLine();
        
        // switch conditions
        switch(color){
            case "red":
                System.out.println("You entered red.");
                break;
                
            case "blue":
                System.out.println("You entered blue.");
                break;
                
            case "green":
                System.out.println("You entered green.");
                break;
            
            // Default value    
            default:
                System.out.println("You entered an unrecognized color.");
                break;
        }
    }
}

Output for Case red:

Output:

You entered red

Output for Case blue:

Output:

You entered blue

Output for Case green:

Output:

You entered green

Output for default Case :

Output:

You entered an unrecognized color.

In the above program, The user enters a color name using the scanner class and then use a switch statement to determine which color was entered. If the user enters “red”, “blue”, or “green”, then the program will print a message indicating the color that was entered. If the user enters any other color, then the program will print a message indicating that the color is unrecognized.

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription