2

I want to ask about switch case statement in javascript.

     switch(ch){
      case 0:
              //do something, if condition match ,so go to case 2 and 3 (no need to go case 1)
             //if not match, go to case 1, 2, and 3
    break;
      case 1:
             //..
    break;
      case 2:
             //..
    break
      case 3:
             //...

   }

In my code has 4 cases . There is a condition in case 0 that will skip case 1 and go to case 2. How can I do that?

Fyp16
  • 131
  • 1
  • 5
  • 16

4 Answers4

1

The switch statement is an alternative to long if else statements (See the docs here). In your case, I believe that you should use the regular if statements.

// check if it passes case1
if (condition === case1) {
  // check if it passes case1
  if (condition === case2) {
     // check if it passes case1
     if (condition === case3) {
       // do something here...
     }
  }
}

You may also use ternary operator, although it might be a bit hard to read as you add more conditions.

Mμ.
  • 8,382
  • 3
  • 26
  • 36
1

i think if else statement better suit your requirement. if you still want to do it in switch here's example :) :

var sw = function(cs){
  switch(cs){
  case 1:
      console.log("case 1 !!!");
      sw(3);
    break;
  case 2:
      console.log("case 2 !!!");
    break;
  case 3:
      console.log("case 3 !!!");
    break;
        }
};
sw(1);
krozero
  • 5,929
  • 3
  • 21
  • 33
0

I believe this's what you are looking for:

function Switcher(choice){ 
    switch(choice){
       case 1: console.log(1);;
       case 4: console.log(4); break;
       case 2: console.log(2); break;
       case 3: console.log(3); break;
    }
}

and then call Switcher(1) and see the O/P

manish
  • 1,450
  • 8
  • 13
0

I was looking at some logic related to switches in JavaScript today, the code I was looking at used a series of if and else statements however there were a bunch of shared logic cases which could be consolidated.

Additionally if and else statements are not exactly equal to switch statements because the runtime may implement them with jump tables making the order of execution faster than if and else.

Because you can only continue iteration patterns in ECMAScript you can hack up a solution which looks like jumping by encapsulating the logic in a fake loop like so:

(function(){

//In some function use this code

var test = 2;

Switch: while(true) switch(test){

case 2: test = 1; continue Switch;
case 1: test = 0; continue Switch;
default:alert(test);return;
};

//End code example

})();

The condition for while(true) can be changed to use another variable for state if you need to.

This gets the code as close to using jump tables as you can in other languages and a similar pattern can implement things like goto or duffs device

See also How can I use goto in Javascript?

Or Porting duff's device from C to JavaScript

Or this GIST https://gist.github.com/shibukawa/315765020c34f4543665

Jay
  • 3,276
  • 1
  • 28
  • 38