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?
2 odpowiedzi
+ 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.... :/
- 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.