You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
If the code for a given label does not have a `break` then it will "fall through"
4
+
to the cases below.
5
+
6
+
This is what makes C-style switches strange. It can occasionaly be useful if the same code should
7
+
run for some or all cases.[^history]
8
+
9
+
```java
10
+
classMain {
11
+
voidsayWhoTheyFought(Stringname) {
12
+
switch (name) {
13
+
case"Goku":
14
+
System.out.println("Fought Pilaf");
15
+
System.out.println("Fought The Red Ribbon Army");
16
+
case"Gohan":// "Goku" will fall through to this case
17
+
System.out.println("Fought Frieza");
18
+
System.out.println("Fought Cell");
19
+
System.out.println("Fought Majin Buu");
20
+
}
21
+
}
22
+
23
+
voidmain() {
24
+
sayWhoTheyFought("Gohan");
25
+
System.out.println("----------------------");
26
+
sayWhoTheyFought("Goku");
27
+
}
28
+
}
29
+
```
30
+
31
+
[^history]: [This StackExchange Post](https://softwareengineering.stackexchange.com/questions/162615/why-dont-languages-use-explicit-fall-through-on-switch-statements) explains how this came about. I don't have a primary source on the "The reason that C did it that way is that the creators of C intended switch statements to be easy to optimize into a jump table." claim, but it lines up with my biases and preconceptions. Therefore it must be true!
0 commit comments