diff --git a/src/img/docs/switch-case-flowchart.svg b/src/img/docs/switch-case-flowchart.svg new file mode 100644 index 00000000..2a9f0d05 --- /dev/null +++ b/src/img/docs/switch-case-flowchart.svg @@ -0,0 +1,42 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/navs/documentation.js b/src/navs/documentation.js index 82a5d30f..c2ad8866 100644 --- a/src/navs/documentation.js +++ b/src/navs/documentation.js @@ -18,5 +18,8 @@ export const documentationNav = { pages['expressions-statements-blocks'], pages['comments'], ], - 'Java Flow Control': [pages['if-else-statement']], + 'Java Flow Control': [ + pages['if-else-statement'], + pages['switch-statement'], + ], } diff --git a/src/pages/docs/switch-statement.mdx b/src/pages/docs/switch-statement.mdx new file mode 100644 index 00000000..00db3b6d --- /dev/null +++ b/src/pages/docs/switch-statement.mdx @@ -0,0 +1,233 @@ +--- +title: Java switch Statement +description: In this tutorial, you will learn to use the switch statement in Java to control the flow of your program’s execution with the help of examples. +--- + +import { TipInfo } from '@/components/Tip' +import NextImage from 'next/image' + +The `switch` statement allows us to execute a block of code among many alternatives. + +The syntax of the `switch` statement in Java is: + +```java +switch (expression) { + + case value1: + // code + break; + + case value2: + // code + break; + + ... + ... + + default: + // default statements + } +``` +## How does the switch-case statement work? + +The `expression` is evaluated once and compared with the values of each case. + +- If `expression` matches with `value1`, the code of `case value1` are executed. Similarly, the code of `case value2` is executed if `expression` matches with `value2`. +- If there is no match, the code of the **default case** is executed. + + + +**Note:** The working of the switch-case statement is similar to the [Java if...else...if ladder](/docs/if-else-statement#3-java-if-else-if-statement). However, the syntax of the `switch` statement is cleaner and much easier to read and write. + + + +### Example: Java switch Statement + +#### Input + +```java +// Java Program to check the size +// using the switch...case statement + +class Main { + public static void main(String[] args) { + + int number = 44; + String size; + + // switch statement to check size + switch (number) { + + case 29: + size = "Small"; + break; + + case 42: + size = "Medium"; + break; + + // match the value of week + case 44: + size = "Large"; + break; + + case 48: + size = "Extra Large"; + break; + + default: + size = "Unknown"; + break; + + } + System.out.println("Size: " + size); + } +} +``` +#### Output: + +```text +Size: Large +``` +In the above example, we have used the switch statement to find the size. Here, we have a variable `number`. The variable is compared with the value of each case statement. + +Since the value matches with **44**, the code of `case 44` is executed. + +```java +size = "Large"; +break; +``` +Here, the `size` variable is assigned with the value `Large`. + + +## Flowchart of switch Statement + +
+ + + +

Flow chart of the Java switch statement

+ +
+ +## break statement in Java switch...case + +Notice that we have been using break in each case block. + +```java + ... +case 29: + size = "Small"; + break; +... +``` +The `break` statement is used to terminate the **switch-case** statement. If `break` is not used, all the cases after the matching case are also executed. For example, + +#### Input + +```java +class Main { + public static void main(String[] args) { + + int expression = 2; + + // switch statement to check size + switch (expression) { + case 1: + System.out.println("Case 1"); + + // matching case + case 2: + System.out.println("Case 2"); + + case 3: + System.out.println("Case 3"); + + default: + System.out.println("Default case"); + } + } +} +``` + +#### Output + +```text +Case 2 +Case 3 +Default case +``` +In the above example, `expression` matches with `case 2`. Here, we haven't used the break statement after each case. + +Hence, all the cases after `case 2` are also executed. + +This is why the `break` statement is needed to terminate the **switch-case** statement after the matching case. To learn more, visit Java break Statement. + +## default case in Java switch-case + +The switch statement also includes an **optional default case**. It is executed when the expression doesn't match any of the cases. For example, + +#### Input + +```java +class Main { + public static void main(String[] args) { + + int expression = 9; + + switch(expression) { + + case 2: + System.out.println("Small Size"); + break; + + case 3: + System.out.println("Large Size"); + break; + + // default case + default: + System.out.println("Unknown Size"); + } + } +} +``` +#### Output + +```text +Unknown Size +``` + +In the above example, we have created a **switch-case** statement. Here, the value of `expression` doesn't match with any of the cases. + +Hence, the code inside the **default case** is executed. + +```java +default: + System.out.println("Unknown Size); +``` +
+ + + +**Note:** The Java switch statement only works with: + + + + \ No newline at end of file pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy