switch and case

george
13 Dec 2013, 03:28
I was reading about the switch and case statements and it seems like you can't test for truthy values with case, like you would in an if statement. Is that true?

In other words I would want to do something like,


number = GetRandomInt(0, 10)
switch (number) {
case (number < 5) {
msg ("Foooooo")
}
case (number > 4) {
msg ("Barrrrrr")
}
}


But it doesn't seem like that's possible (without specifically writing out all the values in each case statement). Or am I missing something?

Why does case work like this if so? It seems like it'd be really handy to have just a boolean test in case.

Liam315
13 Dec 2013, 04:56
I realize this may just be because you've used a simplified example, but if you're accounting for a range of values, why not just use if/else statements?

Because switches need to be specific values, you could try and do a mathematical something to resolve a range into a single digit before invoking the switch. e.g.
number = GetRandomInt(0, 99)
number = number / 10
switch (number) {
case (0,1,2,3,4) {
msg ("Foooooo")
}
case (5,6,7,8,9) {
msg ("Barrrrrr")
}
}

(When dividing integers rather than doubles, the remainder is ignored rather than rounded up or down.)

This is a basic example where your ranges are equal, but depending on what your doing you should be able to manipulate the number into a singular case before invoking the switch. Whether this is tidier and more efficient that simply using the if/else statements is up to you though.

george
13 Dec 2013, 15:33
Thanks Liam, you're right if/else works here, I was just curious about Quest's case, since it works differently than what I'm used to.

jaynabonne
13 Dec 2013, 22:16
I almost hesitate to throw this out, because it's a bit of an abomination, but I can't help myself... :lol:

When you have a switch/case statement, Quest will run down the list of case statements and see which match the value in the switch part. The first one that matches is executed. Quest is different from other languages I've used in that the values in the case expressions don't need to be constant, and they don't need to be unique. So you can use strings, expressions (e.g. 3+4), etc.

When I say "don't need to be unique", I must qualify that. You can not have two values that literally look alike. For example, you can't have:

switch (sum) {
case(7) {
msg("first 7")
}
case(7) {
msg("second 7")
}
}

Quest will complain about duplicate keys. However, you *can* have this:
switch (sum) {
case(7) {
msg("first 7")
}
case(3+4) {
msg("second 7")
}
}

And that will execute properly. Of course, there's no point, as the first 7 means that 3+4 will never evaluated. But it does mean there is a way to do what you want, which is a sort of "obscured if". You almost had it with your statement about Boolean values. The trick is that Boolean values only have two values, true and false. So if you want to have "truthy" values in your cases, then you need to have "truthy" values in your switch - in other words, either true or false.

This is bizarre, but it works:

number = GetRandomInt(0, 10)
switch (true) {
case (number < 5) {
msg ("Foooooo")
}
case (number > 4) {
msg ("Barrrrrr")
}
}

What it does differently is that the switch value is simply "true". This means the switch statement will search for the first case statement that evaluates to true. It's really non-standard, in terms of what I suspect Alex intended, but it does work!

george
14 Dec 2013, 04:31
Ahh, it's so right in your face yet hard to see! Brilliant deduction Jay :).