The switch
keyword in Java is used to execute one block of code out of many based on the value of a variable or expression. It is an alternative to using multiple if-else
statements and makes the code more readable and organized.
Table of Contents
- Introduction
switch
Keyword Syntax- Understanding
switch
- Examples
- Basic Usage
- Using
switch
with Strings - Using
switch
with Enums
- Real-World Use Case
- Conclusion
Introduction
The switch
statement allows you to choose from multiple options based on the value of a variable or expression. It simplifies the code by replacing multiple if-else
statements and makes it more readable.
switch Keyword Syntax
The syntax for the 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 switch
The switch
statement evaluates the expression once and compares it with the values of each case
. If a match is found, the corresponding block of code is executed. The break
statement is used to exit the switch block. If no match is found, the default
block is executed.
Key Points:
- The
expression
must be of typebyte
,short
,int
,char
,String
, or an enum. - The
break
statement is used to terminate acase
block. Withoutbreak
, execution will continue to the nextcase
. - The
default
case is optional and is executed if no matchingcase
is found.
Examples
Basic Usage
To demonstrate the basic usage of the switch
keyword, we will print the name of the day based on the value of the variable day
.
Example
public class SwitchExample { 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 switch
with Strings
The switch
statement can also be used with strings.
Example
public class SwitchStringExample { 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 switch
with Enums
The switch
statement can also be used with enums.
Example
public class SwitchEnumExample { 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 User Input
In real-world applications, the switch
statement can be used to handle user input and execute different actions based on the input.
Example
import java.util.Scanner; public class SwitchUserInput { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.println("Enter a command (start, stop, pause): "); String command = scanner.nextLine(); switch (command.toLowerCase()) { case "start": System.out.println("Starting the process..."); break; case "stop": System.out.println("Stopping the process..."); break; case "pause": System.out.println("Pausing the process..."); break; default: System.out.println("Invalid command."); break; } scanner.close(); } }
Conclusion
The switch
keyword in Java is used for selecting one of many blocks of code to execute based on the value of a variable or expression. It simplifies the code and makes it more readable compared to multiple if-else
statements. By understanding and using the switch
statement, you can efficiently handle multiple conditions and improve the organization of your Java programs.