Skip to content

Challenge Multiple Identical Options In Switch Statements

SaintPeter edited this page · 2 revisions
Clone this wiki locally

Challenge: Multiple Identical Options in Switch Statements

If the break statement is ommitted from a switch statement's case, the following case statement(s) are executed until a break is encountered. If you have multiple inputs with the same output, you can represent them in a switch statement like this:

switch(val) {
  case 1:
  case 2:
  case 3:
    result = "1, 2, or 3";
    break;
  case 4:
    result = "4 alone";
}

Cases for 1, 2, and 3 will all produce the same result.

Something went wrong with that request. Please try again.