# Java Help Case Statements Please

**URL:** <https://forum.codewithmosh.com/t/java-help-case-statements-please/5550>\
**Category:** Java\
**Created:** [June 16, 2021, 10:08am UTC](https://forum.codewithmosh.com/t/java-help-case-statements-please/5550 "2021-06-16T10:08:34Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![harleengulati03](https://avatars.discourse-cdn.com/v4/letter/h/9d8465/32.png) [@harleengulati03](https://forum.codewithmosh.com/u/harleengulati03)\
**Post date:** [June 16, 2021, 10:08am UTC](https://forum.codewithmosh.com/t/java-help-case-statements-please/5550/1 "2021-06-16T10:08:35Z")

</div>

Hey, any help would be greatly appreciated.

For case statements, we add a break after each case else the code would run all the subsequent cases. Does anyone know why this is the case? Surely if we have the code below:

String role = “admin”;  
switch (role) {  
case “admin”:  
System.out.println(“You’re an admin”);  
break;

```
        case "moderator":
            System.out.println("You're a moderator");
            break;

        default:
            System.out.println("You're a guest");

```

and if we got rid of the break statements, surely Java would know that the moderator case and default case does not apply?

In essence, I’m wondering why it runs the other cases when the role is defined as admin when we remove the break statement. Apologises if this is a silly question, thanks for your time and help!

---

<div class="post-metadata">

**Author:** ![eelsholz](https://avatars.discourse-cdn.com/v4/letter/e/439d5e/32.png) [@eelsholz](https://forum.codewithmosh.com/u/eelsholz)\
**Post date:** [June 17, 2021, 2:09am UTC](https://forum.codewithmosh.com/t/java-help-case-statements-please/5550/2 "2021-06-17T02:09:39Z")

</div>

One advantage of this “fall through” behavior is that we can do things like

```auto
switch (role) {
  case "admin":
  case "super-admin":
    System.out.println("You're some kind of admin");
    break;
  case "moderator":
    System.out.println("You're a moderator");
    break;
  default:
    System.out.println("You're a guest");
}

```

The “admin” case falls through to the “super-admin” case and allows us to execute logic that is appropriate for any sort of admin when the role is “admin” or “super-admin”.

---

<div class="post-metadata">

**Author:** ![jmrunkle](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.codewithmosh.com/jmrunkle/32/3765_2.png) [@jmrunkle](https://forum.codewithmosh.com/u/jmrunkle)\
**Post date:** [August 18, 2022, 3:21am UTC](https://forum.codewithmosh.com/t/java-help-case-statements-please/5550/3 "2022-08-18T03:21:18Z")

</div>

Also, Java 14 introduced the permanent feature of the new “enhanced” switch-case which has the behavior you want:

```java
switch (role) {
  case "admin", "super-admin" -> System.out.println("You're some kind of admin");
  case "moderator" -> System.out.println("You're a moderator");
  default -> System.out.println("You're a guest");
}

```

It is documented here:

> **[Java Language Updates](https://docs.oracle.com/en/java/javase/14/language/switch-expressions.html)**
>
> Like all expressions, switch expressions evaluate to a single value and can be used in statements. They may contain "case L -\>" labels that eliminate the need for break statements to prevent fall through. You can use a yield statement to specify...
