/    Sign up×
Community /Pin to ProfileBookmark

Switch behaviour

Dear users,

I have a JS switch:
switch(var) {
case 1:
do something 1
break;
case 2:
do something 2
case 1:
case 3:
default:
do other
break;
}

But it doesn’t work as I want. As you can see, I want to execute more than one case if the var == 1, but JS executes only “do something 1”, and I want it executes also “do other”.

How can I do?

Thanks

to post a comment
JavaScript

7 Comments(s)

Copy linkTweet thisAlerts:
@rootNov 30.2018 — 
  • 1. when posting, please use </code></span></C> and <C><span><code> tags arounf the blocks of code you have posted, makes them readable.


  • var is a reserved word, never use reserved keywords or names of variables that are already system variables.

    var p = 1;
    switch( p ){
    case 1:
    console.log("p = %s",p);
    break;
    case 2:
    case 3:
    console.log("p = %s",p);
    break;
    default:
    console.log("Default");
    }


    if you had p==1 then you are creating a truth condition that switch case is not designed to handle as you get very strange results and unpredictable results doing that.

    Had you read up how to use a switch case statement, you would not be venturing off in to this realm of shooting in the dark with items like p==1.

    If you want to test a condition then you should do it in an if conditional statement block and not a switch..case block.
    Copy linkTweet thisAlerts:
    @redagrauthorDec 04.2018 — Is there a way to execute two cases of switch if the variable satisfies them?

    Thanks
    Copy linkTweet thisAlerts:
    @rootDec 05.2018 — Well really it all boils down to what it is you are trying to do.

    Various statement blocks exist to assist with and satisfy needs of coding and have in many cases established and particular uses.

    I have seen people use Switch..case where if..else should be use and what it to do whatever it is when there are articles saying DON@T DO IT because the behaviour is not predictable, what happens on your machine does not necessarily happen on the clients machine.

    So if you need TWO conditions satisfied, you really should be using the if..elseif..else conditionals where you can evaluate true and false statements.

    if( conditionX&gt;=10 &amp;&amp; conditionX&lt;=100){
    // do something here
    }else{
    // otherwise do something here
    }


    and if your condition only need two conditions checked
    conditionX = 10;
    conditionA = 0.5;
    if( conditionX&gt;=10 &amp;&amp; conditionA&gt;=1){
    // do something here
    console.log("Condition 1 met");
    }elseif( conditionX&lt;=10 &amp;&amp; conditionA&lt;=0 ){
    // otherwise do something here
    console.log("Condition 2 met");
    }else{
    // we get here, neither conditions are true...
    console.log("Neither condition met");
    }

    What do you recon is going to happen?

    You can have more than one conditional test in an if statement, you just need to use an operator to compare it with or its result with, so using brackets around complex conditionals is important.

    if( (!a || !b) &amp;&amp; false ){
    // do something
    }
    to return true, the result of a or b need to be false to return a true from the result of not, so if is !a or !b are true and both not's make false, the result would be false, when that is AND with false, it results in true...

    Confused?

    You can build very weird condition tests with an if that you can not do with a switch and people have tried and used and all the guides I have read have a DON'T DO IT because of known issues over how conditionals and boolean logic evaluates within a switch, it is only really meant to test one variable value against a list, making that list a useful way of checking for specific values.

    [code]switch( numb ){
    case 2:
    case 4:
    case 6:
    case 8:
    case 10: console.log("Even number 10 of under");
    break;
    case 1:
    case 3:
    case 5:
    case 7:
    case 9: console.log("Even number 10 of under");
    break;
    default:
    console.log("A number not in the specific test range");
    }[code]
    so it is a very simplistic example, much more suited to looking for a strings IMHO.

    So your needs are based on what you are attempting and to use the appropriate method and going by the limited information you have provided, lack of any useful code to assist in directing you, I will say you need the if..else if..else clauses in place.
    Copy linkTweet thisAlerts:
    @redagrauthorDec 10.2018 — I didn't explain well. I want to write "case 1" more than once in the switch:
    switch(my_var) {
    case 1:
    // do something 1
    break;
    case 2:
    // do something 2
    case 1:
    case 3:
    default:
    // do other
    break;
    }

    As you can see, the "case 1" are written twice, and if my_var == 1, I want to execute "// do something 1" and "// do other", one after the other.

    Is it possible?

    Thanks
    Copy linkTweet thisAlerts:
    @JMRKERDec 11.2018 — Can you re-write the order?
    <i>
    </i>switch(my_var) {
    case 2:
    // do something 2
    break;
    case 1:
    // do something 1
    case 3: // no need for this test if goes to default anyway
    default:
    // do other
    break;
    }
    Copy linkTweet thisAlerts:
    @rootDec 11.2018 — You can not have more than one case in a list.

    also note that case 1: and case "1": are different because one is a number and another is a string, that is the only case I can think of that wouold allow multiple cases with the same numeric, one as a number, one as a string.
    Copy linkTweet thisAlerts:
    @redagrauthorDec 13.2018 — @JMRKER#1598830

    Unfortunately not, because the real code is much more complex than this example written here.

    @root#1598837

    So if I write case 1, and after case "1" can I execute twice a case in a switch?

    Thanks
    ×

    Success!

    Help @redagr spread the word by sharing this article on Twitter...

    Tweet This
    Sign in
    Forgot password?
    Sign in with TwitchSign in with GithubCreate Account
    about: ({
    version: 0.1.9 BETA 4.24,
    whats_new: community page,
    up_next: more Davinci•003 tasks,
    coming_soon: events calendar,
    social: @webDeveloperHQ
    });

    legal: ({
    terms: of use,
    privacy: policy
    });
    changelog: (
    version: 0.1.9,
    notes: added community page

    version: 0.1.8,
    notes: added Davinci•003

    version: 0.1.7,
    notes: upvote answers to bounties

    version: 0.1.6,
    notes: article editor refresh
    )...
    recent_tips: (
    tipper: @Yussuf4331,
    tipped: article
    amount: 1000 SATS,

    tipper: @darkwebsites540,
    tipped: article
    amount: 10 SATS,

    tipper: @Samric24,
    tipped: article
    amount: 1000 SATS,
    )...