If I don't use a "break" statement, can a single "switch" statement execute multiple "case" statements? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

If I don't use a "break" statement, can a single "switch" statement execute multiple "case" statements?

For example: int day = 2; switch(day) { case >= 1: System.out.println("Monday"); case >= 2: System.out.println("Tuesday"); case >= 3: System.out.println("Wednesday"); } Is this allowed and would it print "MondayTuesday" but not Wednesday?

21st Nov 2016, 7:12 PM
Matthew
Matthew - avatar
2 Answers
+ 3
yup, that is correct, and would print exactly as you described (but with new line between them). it is allowed and can come in handy in some cases (like printing stages of a some process and wanting to not start from stage one for instance) what is not allowed tho is the '>=' between each case and number.... :/
21st Nov 2016, 7:29 PM
Burey
Burey - avatar
- 1
This are allowed. But it will prints Monday, Tuesday and Wednesday. Adding break statement after System.out.println("Tuesday"); to print just Monday and Tuesday.
21st Nov 2016, 11:06 PM
The PGDeveloper
The PGDeveloper - avatar