No, you donβt have to. However, if you omit the break statement, all other statements inside the switch block are executed regardless of the case value that they are testing with.
This can sometimes lead to undesirable results, as in the following code:
switch (grade) { case 'A': System.out.println("You got an A!"); //Notice the lack of a 'break' statement case 'B': System.out.println("You got a B!"); case 'C': System.out.println("You got a C."); case 'D': System.out.println("You got a D."); default: System.out.println("You failed. :("); }
If you set the grade variable to "A", this will be your result:
You got an A! You got a B. You got a C. You got a D. You failed. :(
source share