Your question is probably a duplicate of something else, but the reason is that case
statement within a Java switch
by default will flow onto the next case
statement unless a break
be explicitly mentioned. To better understand why the case statements behave this way, an example would make this clear. Let’s say that you wanted the same logic to happen for Monday, Tuesday, and Wednesday. Then, you could use the following:
switch (day) {
case 1:
case 2:
case 3:
System.out.println("Today is Monday, Tuesday, or Wednesday");
break;
case 4:
System.out.println("Today is Thursday");
break;
case 7:
System.out.println("Today is Sunday");
break;
}
solved Java switch runs all cases if no break