The case keyword in Java is used within a switch statement to define a specific block of code that should be executed when the value of the switch expression matches the value specified in the case label.
Table of Contents
- Introduction
caseKeyword Syntax- Understanding
case - Examples
- Basic Usage
- Using
casewith Strings - Using
casewith Enums
- Real-World Use Case
- Conclusion
Introduction
The case keyword is an integral part of the switch statement in Java. It allows you to specify different actions for different values of a given variable or expression. Each case label is followed by the code that should be executed if the switch expression matches the case value.
case Keyword Syntax
The syntax for using the case keyword within a switch statement is as follows:
switch (expression) {
case value1:
// code to be executed if expression equals value1
break;
case value2:
// code to be executed if expression equals value2
break;
// you can have any number of case statements
default:
// code to be executed if expression doesn't match any case
break;
}
Example:
int day = 3;
switch (day) {
case 1:
System.out.println("Monday");
break;
case 2:
System.out.println("Tuesday");
break;
case 3:
System.out.println("Wednesday");
break;
default:
System.out.println("Invalid day");
break;
}
Understanding case
Each case label specifies a value that the switch expression is compared to. If the switch expression matches a case value, the code following that case label is executed until a break statement is encountered or the end of the switch block is reached.
Key Points:
- Each
casevalue must be unique within the sameswitchstatement. - The
breakstatement is used to terminate acaseblock. Withoutbreak, execution will continue to the nextcase. - The
defaultcase is optional and is executed if no matchingcaseis found.
Examples
Basic Usage
To demonstrate the basic usage of the case keyword, we will print the name of the day based on the value of the variable day.
Example
public class CaseExample {
public static void main(String[] args) {
int day = 3;
switch (day) {
case 1:
System.out.println("Monday");
break;
case 2:
System.out.println("Tuesday");
break;
case 3:
System.out.println("Wednesday");
break;
case 4:
System.out.println("Thursday");
break;
case 5:
System.out.println("Friday");
break;
case 6:
System.out.println("Saturday");
break;
case 7:
System.out.println("Sunday");
break;
default:
System.out.println("Invalid day");
break;
}
}
}
Using case with Strings
The case keyword can also be used with strings.
Example
public class CaseStringExample {
public static void main(String[] args) {
String fruit = "Apple";
switch (fruit) {
case "Apple":
System.out.println("It's an apple.");
break;
case "Banana":
System.out.println("It's a banana.");
break;
case "Cherry":
System.out.println("It's a cherry.");
break;
default:
System.out.println("Unknown fruit.");
break;
}
}
}
Using case with Enums
The case keyword can also be used with enums.
Example
public class CaseEnumExample {
enum Day {
MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY
}
public static void main(String[] args) {
Day day = Day.WEDNESDAY;
switch (day) {
case MONDAY:
System.out.println("Monday");
break;
case TUESDAY:
System.out.println("Tuesday");
break;
case WEDNESDAY:
System.out.println("Wednesday");
break;
case THURSDAY:
System.out.println("Thursday");
break;
case FRIDAY:
System.out.println("Friday");
break;
case SATURDAY:
System.out.println("Saturday");
break;
case SUNDAY:
System.out.println("Sunday");
break;
default:
System.out.println("Invalid day");
break;
}
}
}
Real-World Use Case
Handling Menu Selections
In real-world applications, the case keyword is useful for handling menu selections or user commands.
Example
import java.util.Scanner;
public class CaseMenuExample {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Menu:");
System.out.println("1. Start");
System.out.println("2. Stop");
System.out.println("3. Pause");
System.out.println("Enter your choice: ");
int choice = scanner.nextInt();
switch (choice) {
case 1:
System.out.println("Starting the process...");
break;
case 2:
System.out.println("Stopping the process...");
break;
case 3:
System.out.println("Pausing the process...");
break;
default:
System.out.println("Invalid choice.");
break;
}
scanner.close();
}
}
Conclusion
The case keyword in Java is an essential part of the switch statement, allowing you to specify different blocks of code to be executed based on the value of a variable or expression. By understanding and using the case keyword, you can efficiently handle multiple conditions and improve the readability and organization of your Java programs.