Hey, any help would be greatly appreciated.
For case statements, we add a break after each case else the code would run all the subsequent cases. Does anyone know why this is the case? Surely if we have the code below:
String role = “admin”;
switch (role) {
case “admin”:
System.out.println(“You’re an admin”);
break;
case "moderator":
System.out.println("You're a moderator");
break;
default:
System.out.println("You're a guest");
and if we got rid of the break statements, surely Java would know that the moderator case and default case does not apply?
In essence, I’m wondering why it runs the other cases when the role is defined as admin when we remove the break statement. Apologises if this is a silly question, thanks for your time and help!