Removing mandatory break
from switch cases
#4393
Replies: 4 comments 21 replies
-
Yes, the |
Beta Was this translation helpful? Give feedback.
-
Switch fallthrough never reevaluates the condition. The switch jumps to the first case based on the expression and then executes all following statements until it hits a Example in Java: public class JavaFiddle
{
public static void main(String[] args)
{
int i = 1;
switch (i) {
case 1: System.out.println("Hello");
case 2: System.out.println("World");
default: System.out.println("Fallthrough");
}
}
} prints:
Example in C: #include <stdio.h>
int main()
{
int i = 1;
switch (i) {
case 1: printf("Hello\n");
case 2: printf("World\n");
default: printf("Fallthrough\n");
}
} prints:
|
Beta Was this translation helpful? Give feedback.
-
It remains to specify whether omitting the |
Beta Was this translation helpful? Give feedback.
-
In the early days, most C# programmers come from C/C++/Java, and a lot of code are ported from C/C++/Java. The strictness of I wish |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
I'm not even sure why it's there... but I have examples of code that could be a whole lot cleaner if I could skip the break statement.
Any reason for why it's mandatory?
Beta Was this translation helpful? Give feedback.
All reactions