multiple expressions in one switch statement
Asked Answered
G

3

5

It is my first time using the switch statement in Javascript. Is there a way to evaluate multiple conditions one switch statement, like so:

var i = 1;
switch(i && random(1)<0.3) {
    case (1):
        //code block
         break;
    case (2):
        //code block
} 

So that the code blocks would execute if both of the conditions were true?

Gastrectomy answered 24/7, 2016 at 20:31 Comment(2)
So that the code blocks would execute if both of the conditions were true: both of them? When should only the first be executed, when only the second, and when none of them?Bundesrat
You might want to use + instead of &&Orchitis
B
0

You could do something like this:

var i = 1;
switch((i==1) + (Math.random(1)<0.3)*2) {
    case 0:
        //code block when neither is true
         break;
    case 1:
        //code block when only i == 1
         break;
    case 2:
        //code block when only random(1)<0.3
         break;
    case 3:
        //code block when both i==1 and random(1)<0.3
        break;
} 

... but it is not really the nicest code, and can easily lead to mistakes when one of the tested expressions is anything else than 0 or 1 (false or true).

It is better to use if ... else constructs to deal with this:

var i = 1;
var small = Math.random(1)<0.3;
if (i==1) {
    if (small) {
        //code block when both i==1 and random(1)<0.3
    } else {
        //code block when only i == 1
    }
} else if (small) {
    //code block when only random(1)<0.3
} else {
    //code block when neither is true
}
Bundesrat answered 24/7, 2016 at 20:42 Comment(0)
P
8

It's possible to write switch statements this way:

switch (true) {
  case a && b:
    // do smth
    break;
  case a && !b:
    // do other thing
    break;
}

The only thing you need to keep in mind is that && can return not only boolean, but rather any other value if e.g. 'a' (in code snippet above) resolves to some false value. If 'b' is string - then a && b, where a is false, shall return a string.
So when you use this pattern always ensure that the right side of && expression resolves to a boolean.

Platter answered 24/7, 2016 at 21:4 Comment(0)
B
0

You can do that but the switch statement will switch on the result of the expression you provide.

Given you have a logical and (&&) in your expression there are two possible outcomes defined on how && works.

  1. if the left hand expression evaluates to true the expression will be equal to the evaluation of the second part.
  2. if the left hand expression evaluates to false the whole expression will evaluate to false.

You can read more about the switch statement on the Ecmascript spec

Bolton answered 24/7, 2016 at 20:33 Comment(5)
Actually && can evaluate to non-boolean results, but yes it's true that the current script will never match any of the casesOrchitis
I think I'd have to say case(5 && true)Gastrectomy
@Gastrectomy again the same apply to the case statement block as described in the two points above.Bolton
I see what you mean on multiple boolean expressions evaluating to either true or false. But what other boolean expressions can there be?Gastrectomy
@Gastrectomy if you read number 1 and 2 of my answer you will get the idea. && will return false if the left hand of && is false. it will return the evaluation of the right hand otherwise. false && 4 -> false, true && 5 -> 5Bolton
B
0

You could do something like this:

var i = 1;
switch((i==1) + (Math.random(1)<0.3)*2) {
    case 0:
        //code block when neither is true
         break;
    case 1:
        //code block when only i == 1
         break;
    case 2:
        //code block when only random(1)<0.3
         break;
    case 3:
        //code block when both i==1 and random(1)<0.3
        break;
} 

... but it is not really the nicest code, and can easily lead to mistakes when one of the tested expressions is anything else than 0 or 1 (false or true).

It is better to use if ... else constructs to deal with this:

var i = 1;
var small = Math.random(1)<0.3;
if (i==1) {
    if (small) {
        //code block when both i==1 and random(1)<0.3
    } else {
        //code block when only i == 1
    }
} else if (small) {
    //code block when only random(1)<0.3
} else {
    //code block when neither is true
}
Bundesrat answered 24/7, 2016 at 20:42 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.