Switch statement in java example

Anna 20 Published: 09/28/2024

Switch statement in java example

I apologize for any confusion earlier. Here is a Java example of a switch statement:

public class Main {

public static void main(String[] args) {

int day = 2;

switch (day) {

case 1:

System.out.println("Today is Monday");

break;

case 2:

System.out.println("Today is Tuesday");

break;

case 3:

System.out.println("Today is Wednesday");

break;

default:

System.out.println("Invalid day of the week");

}

}

}

In this example, we are using a switch statement to determine which message to print based on the value of the day variable. The possible values for the day variable are 1 (Monday), 2 (Tuesday), and 3 (Wednesday). If the day variable has any other value, the default case is executed.

Here's how it works:

The program checks if the day variable is equal to 1. Since day is not 1, the program moves on to the next case. It checks if day is equal to 2. Since day is indeed 2, the program prints "Today is Tuesday" and then breaks out of the switch statement using the break keyword. If none of the cases match (like in this example), the program will execute the default case and print "Invalid day of the week".

Here's a more complex example that demonstrates the use of variables inside a switch statement:

public class Main {

public static void main(String[] args) {

int day = 2;

String message;

switch (day) {

case 1:

message = "Today is Monday";

break;

case 2:

message = "Today is Tuesday";

break;

case 3:

message = "Today is Wednesday";

break;

default:

message = "Invalid day of the week";

}

System.out.println(message);

}

}

In this example, we are using a switch statement to determine which message to print based on the value of the day variable. The possible values for the day variable are 1 (Monday), 2 (Tuesday), and 3 (Wednesday). If the day variable has any other value, the default case is executed.

Here's how it works:

The program checks if the day variable is equal to 1. Since day is not 1, the program moves on to the next case. It checks if day is equal to 2. Since day is indeed 2, the program sets the message variable to "Today is Tuesday" and then breaks out of the switch statement using the break keyword. If none of the cases match (like in this example), the program will execute the default case and set the message variable to "Invalid day of the week". Finally, the program prints the value stored in the message variable.

I hope this helps! Let me know if you have any questions or need further clarification.

How to write expression in switch case in Java?

I'm happy to help you with that!

In Java, there is no direct equivalent of a switch statement like in C or Python. Instead, you can use a series of if-else statements or polymorphism to achieve similar functionality.

However, if you're looking for a more concise way to handle different cases, you can create a simple method that takes an object as a parameter and uses a chain of if-else statements to determine the appropriate action. This is often referred to as the "command pattern" or "strategy pattern".

Here's an example:

public class ExpressionEvaluator {

public void evaluate(String expression) {

switch (expression) {

case "CASE1":

// code for CASE1

break;

case "CASE2":

// code for CASE2

break;

case "CASE3":

// code for CASE3

break;

default:

// unknown case, handle error

}

}

}

In the above example, we have a switch statement that takes a string expression as input and performs different actions based on the value of the expression. The break statements are used to exit the switch block.

However, if you want to write this in a more Java-like way, you can use polymorphism and create an enum for your expressions:

public enum Expression {

CASE1, CASE2, CASE3;

public void evaluate() {

switch (this) {

case CASE1:

// code for CASE1

break;

case CASE2:

// code for CASE2

break;

case CASE3:

// code for CASE3

break;

}

}

}

In this example, we define an enum Expression with three values: CASE1, CASE2, and CASE3. Each value of the enum has a corresponding evaluate() method that performs different actions based on the expression.

This approach allows you to write more concise code and makes it easier to extend your program if you need to add more expressions in the future.