-1

What I'm trying to do is allow a user to input different variations of a string(by that I mean, a user could enter either "Calculator", "Calc", "calculator", etc.), and still get the same code executed so I don't have to copy it into each case I want to do the same thing. i.e.

switch(input) {
    case "string1":
        statement 1;
        break;
    case "string2":
        statement 1;
        break;
    case "string3":
        statement 2;
        break;
    case "string4":
        statement 2;
        break;
    default:
        break;

Is there an easy way to do this, other than writing the same statement for each case?

Ivan G.
  • 1
  • 3

1 Answers1

0

A comment gave me the term I was looking for, so I searched it up and found the answer myself. I now know that I can combine cases by putting the case label with just the colons above the next case, that has the statement to be run in both cases. i.e.

switch(input) {
    case "string1":
    case "string2":
        statement1;
        break;
    case "string3":
    case "string4":
        statement2;
        break;
    default:
        break;

This gives my desired output. Thank you, Dave Newton!

Ivan G.
  • 1
  • 3