String in Switch Statement

In many programming languages, including Java and C++, a switch statement allows you to test the value of a variable and execute different code blocks depending on the value. Typically, the variable being tested is an integer or an enumerated type, but in some languages, such as JavaScript, it is also possible to use a string as the variable being tested in a switch statement.

For example, in JavaScript, you could use a switch statement to perform different actions based on the value of a string variable:

let color = "red";

switch (color) {
  case "red":
    console.log("The color is red.");
    break;
  case "green":
    console.log("The color is green.");
    break;
  case "blue":
    console.log("The color is blue.");
    break;
  default:
    console.log("Unknown color.");
}

In this example, the switch statement checks the value of the color variable and executes the appropriate code block based on its value. If color is “red”, the first case block is executed and the console logs “The color is red.” If color is “green”, the second case block is executed and the console logs “The color is green.” If color is “blue”, the third case block is executed and the console logs “The color is blue.” If color is any other value, the default block is executed and the console logs “Unknown color.”

It’s worth noting that using a string as the variable being tested in a switch statement can be less efficient than using an integer or an enumerated type, especially if the string has a large number of possible values. Additionally, not all programming languages support using a string in a switch statement, so it’s important to check the documentation for your particular language to see what is allowed.

String in Switch Statement Example 1:

Here is an example of a Java program that uses a switch statement with a string variable:

public class StringSwitchExample {
    public static void main(String[] args) {
        String fruit = "banana";
        switch (fruit) {
            case "apple":
                System.out.println("Selected fruit is apple");
                break;
            case "orange":
                System.out.println("Selected fruit is orange");
                break;
            case "banana":
                System.out.println("Selected fruit is banana");
                break;
            default:
                System.out.println("Selected fruit is not apple, orange, or banana");
                break;
        }
    }
}
public class StringSwitchExample {
    public static void main(String[] args) {
        String fruit = "banana";
        switch (fruit) {
            case "apple":
                System.out.println("Selected fruit is apple");
                break;
            case "orange":
                System.out.println("Selected fruit is orange");
                break;
            case "banana":
                System.out.println("Selected fruit is banana");
                break;
            default:
                System.out.println("Selected fruit is not apple, orange, or banana");
                break;
        }
    }
}

In this example, the fruit variable is a string that is used in a switch statement. The switch statement checks the value of fruit and executes the appropriate code block based on its value. If fruit is “apple”, the first case block is executed and the console logs “Selected fruit is apple”. If fruit is “orange”, the second case block is executed and the console logs “Selected fruit is orange”. If fruit is “banana”, the third case block is executed and the console logs “Selected fruit is banana”. If fruit is any other value, the default block is executed and the console logs “Selected fruit is not apple, orange, or banana”.

String in Switch Statement Example 2:

Here is another example of a C++ program that uses a switch statement with a string variable:

#include <iostream>
#include <string>
using namespace std;

int main() {
    string day = "Monday";
    switch (day[0]) {
        case 'M':
            cout << "Today is Monday";
            break;
        case 'W':
            cout << "Today is Wednesday";
            break;
        case 'F':
            cout << "Today is Friday";
            break;
        default:
            cout << "Today is not Monday, Wednesday, or Friday";
            break;
    }
    return 0;
}

In this example, the day variable is a string that represents the name of a day of the week. The switch statement checks the value of the first character in the day variable and executes the appropriate code block based on its value. If the first character is ‘M’, the first case block is executed and the console logs “Today is Monday”. If the first character is ‘W’, the second case block is executed and the console logs “Today is Wednesday”. If the first character is ‘F’, the third case block is executed and the console logs “Today is Friday”. If the first character is any other value, the default block is executed and the console logs “Today is not Monday, Wednesday, or Friday”.

Note that in this example, we use the [0] operator to access the first character of the day variable. Since the switch statement only compares individual characters, we only need to check the first character to determine the day of the week.