-2

Is it possible to make like a two layer switch case? Say I have two expressions, that inside the the first case it looks for the second expression? I have googled but I have not found anything that I look for.

switch (Middle, Direction) {
  case true:
    case 'left':
      code block

      break;
    case 'right':
      code block

      break;
    break;
  case false:
    case 'left':
      code block

      break;
    case 'right':
      code block

      break;
    break;

  default:
    code block
}
Funktiona
  • 113
  • 2
  • 11
  • Take a look at [this question](http://stackoverflow.com/questions/15931089/alternative-to-nested-switch-statements-in-java) to see why you don't want to do this. – takendarkk Jul 24 '16 at 11:22
  • You can't just wish new syntax into existence, but in any case your suggested new syntax wouldn't work because the first `case true` would fall through to `case 'left'` as per standard `switch` behaviour. – nnnnnn Jul 24 '16 at 12:47

2 Answers2

1

Yes, you have to use individual, complete switches:

switch (Middle) {
  case true:
    switch (Direction) {
       case 'left':
         //code block
         break;
       case 'right':
         //code block
         break;
    }
    break;
  case false:
    switch (Direction) {
      case 'left':
        //code block
        break;
      case 'right':
        //code block
        break;
    }
    break;
  default:
    //code block
}

Note that in the above, if Direction isn't 'left' or 'right' but Middle is true or false (which, if it's really a boolean, it must be), then no default code is run as the subordinate switches don't have defaults.

Alternately, you might combine Middle and Direction, which makes it easier to use a default for non-matching cases:

switch (Middle+'|'+Direction) {
  case 'true|left':
    //code block
    break;
  case 'true|right':
    //code block
    break;
  case 'false|left':
    //code block
    break;
  case 'false|right':
    //code block
    break;
  default:
    //code block
}

Or look at a dispatch object:

var actions = {
  "true|left": function() {
    // code for Middle = true, Direction = left
  },
  "true|right": function() {
    // code for Middle = true, Direction = right
  },
  "false|left": function() {
    // code for Middle = false, Direction = left
  },
  "false|right": function() {
    // code for Middle = false, Direction = right
  },
  "default": function() {
    // code for the default
  }
};
(actions[Middle+"|"+Direction] || actions.default)();

...though in this specific case it doesn't buy you much of anything over a switch.

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
  • Would you use your second example instead of using if statements in each case? – Funktiona Jul 24 '16 at 11:36
  • @Funktiona: I have done, but only rarely. If it's really just five possibilities as above, I probably wouldn't use switch at all, just `if/else`. – T.J. Crowder Jul 24 '16 at 11:40
0

Just use another switch in your case.

takendarkk
  • 3,347
  • 8
  • 25
  • 37
Alex
  • 566
  • 3
  • 15