diff --git a/content/java/concepts/switch/switch.md b/content/java/concepts/switch/switch.md index f4aa3be82f4..47cc6f0af3e 100644 --- a/content/java/concepts/switch/switch.md +++ b/content/java/concepts/switch/switch.md @@ -3,22 +3,24 @@ Title: 'Switch' Description: 'A switch statement provides a means of checking an expression against various case statements.' Subjects: - 'Computer Science' + - 'Web Development' Tags: - - 'Switch' - 'Conditionals' - 'Control Flow' + - 'Switch' + - 'Values' CatalogContent: - 'learn-java' - 'paths/computer-science' --- -A **`switch`** statement provides a means of checking an expression against various `case` statements. If there is a match, the code within starts to execute. The `break` keyword can be used to terminate a case. +A **`switch`** statement provides a means of checking an expression against various `case` statements. If there is a match, the code within starts to execute. The `break` keyword can be used to terminate a case. There's also an optional `default` statement marking code that executes if none of the `case` statements are true. -There's also an optional `default` statement marking code that executes if none of the `case` statements are true. +The `switch` statement is especially useful when dealing with discrete values, such as enums, characters, and integers. It improves readability and simplifies decision-making in code compared to long chains of `if-else` conditions. ## Syntax -A `switch` statement looks like: +A `switch` statement (or a `switch` case) looks like: ```pseudo switch (expression) { @@ -33,33 +35,133 @@ switch (expression) { } ``` -- The `switch` keyword initiates the statement and is followed by `()`, which contains the value that each case will compare. In the example, the value or expression of the switch statement is `grade`. -- Inside the block, `{}`, there are multiple cases. -- The `case` keyword checks if the expression matches the specified value that comes after it. The value following the first case is `9`. If the value of grade is equal to `9`, then the code that follows the `:` would run. -- The `break` keyword tells the computer to exit the block and not execute any more code or check any other cases inside the code block. -- At the end of each switch statement, there is a `default` statement. If none of the cases are `true`, then the code in the `default` statement will run. It’s essentially the `else` part in an `if`/`else if`/`else` statement. +Here, `expression` is the expression to be checked against the various `case` statements. + +## How Switch Case Works + +1. Java evaluates the `expression` inside the `switch` case. +2. It compares the result with each `case` value. +3. If a match is found, the code block under that `case` is executed. +4. The `break` statement ends the `switch` block. +5. If no match is found, the `default` block (if present) is executed. + +> **Note:** Without `break`, the execution will continue to the next case—this is called fall-through. + +## Switch vs. If-Else -In the code above, suppose grade is equal to `10`, then the output would be “Sophomore”. +| Feature | `switch` Case | `if-else` Statement | +| ------------------------ | ----------------------------------------- | ----------------------------------------- | +| **Use Case** | Best for fixed values or discrete options | Suitable for complex conditions or ranges | +| **Data Types Supported** | `int`, `char`, `String`, `enum` | All data types and expressions | +| **Readability** | More readable for many discrete cases | Less readable with many nested conditions | +| **Fall-through** | Possible if `break` is omitted | No fall-through | +| **Performance** | Slightly better for large case sets | Comparable in most cases | -> **Note:** Without the `break` keyword at the end of each case, the program would execute the code for the first matching case and _all_ subsequent cases, including the `default` code. This behavior is different from `if`/`else` conditional statements which execute only one block of code. +## Example 1: Basic Integer Switch -## Example +This example uses an `int` variable to determine the current day of the week using a `switch` statement. It prints the corresponding weekday based on the numeric value: ```java -int rating = 3; +public class DaySwitch { + public static void main(String[] args) { + int day = 3; -switch (rating) { - case 5: - System.out.println("Exceptional"); - break; - case 4: - System.out.println("Good"); - break; - case 3: - System.out.println("Fair"); - break; - default: - System.out.println("Poor"); - break; + 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("Other day"); + } + } +} +``` + +Here is the output: + +```shell +Wednesday +``` + +## Example 2: Switch with String + +This example shows how to use a string in a `switch` statement to match and identify the month. It's available from Java 7 onwards: + +```java +public class MonthSwitch { + public static void main(String[] args) { + String month = "April"; + + switch (month) { + case "January": + System.out.println("1st month"); + break; + case "April": + System.out.println("4th month"); + break; + default: + System.out.println("Unknown month"); + } + } +} +``` + +Here is the output: + +```shell +4th month +``` + +## Example 3: Enhanced Switch (Java 14+) + +This example demonstrates the enhanced `switch` syntax introduced in Java 14. It uses the new arrow (`->`) syntax for cleaner and safer case handling, and returns a value based on day type: + +```java +public class EnhancedSwitch { + public static void main(String[] args) { + int day = 5; + + String dayType = switch (day) { + case 1, 2, 3, 4, 5 -> "Weekday"; + case 6, 7 -> "Weekend"; + default -> "Invalid day"; + }; + + System.out.println(dayType); + } } ``` + +Here is the output: + +```shell +Weekday +``` + +## Frequently Asked Questions + +### 1. Can `switch` statements be used with strings in Java? + +Yes, from Java 7 onwards, `switch` statements support `String` as a valid type. + +### 2. What happens if `break` is not used? + +Without break, the execution falls through to the next case, which may lead to unexpected behavior. + +### 3. Can a `switch` expression return a value? + +Yes, starting with Java 14, the enhanced `switch` allows returning a value directly using the `->` syntax. + +### 4. Can we use boolean in `switch` statements? + +No, boolean is not supported in Java `switch` statements. Use `if-else` instead. + +### 5. Is the `default` case mandatory? + +No, but it's a good practice to include it to handle unexpected values.