Display operators and ranges of numbers

How do you create a switch statement in as3 to make the register applicable to the entire range of numbers?

if (mcPaddle.visible == true) { switch (score) { case 10://10 to 100 myColor.color = 0x111111; break; case 110://110 to 1000 //etc etc break; } } 

I tried several ways to make the case applicable for all numbers between 10-100 and 110-1000, but cannot find a way to do this, and I cannot find the correct syntax for such a thing in as3.

+8
actionscript-3
source share
4 answers

You can use the switch block:

 var score:Number = 123; switch(true){ case score > 120 && score < 125 : trace('score > 120 && score < 125'); break; case score > 100 && score < 140 : trace('score > 100 && score < 140'); break; case score == 123 : trace('score == 123'); break; } //score > 120 && score < 125 
+15
source share

The ActionScript switch statement does not work with ranges, but you can easily do this with if / else goals:

 if (score >= 10 && score <= 100) { //10 - 100 } else if (score <= 110) { //101 - 110 } else if (score <= 1000) { //111 - 1000 } 
0
source share

switch statements simply update constructs like if (a = b) or (a = c) or (a = d) ... It is not intended for ranges. You can mimic it somewhat using dips:

 switch (score) { case 10: case 11: case 12: case 13: case etc... blah blah blah break; } 

but it's a ridiculously dumb way to go. Much easier / use regular if()

0
source share

For those who are looking for how to use this in HTML / jQuery, I used the @ OXMO456 answer to create this simple pen: http://codepen.io/anon/pen/jHFoB

You just need to set var as usual and delete the lines starting with trace .

Ps. I am adding this as an answer since I do not have enough comments to comment on it. If anyone can, please move / copy it there. Thanks!

0
source share

All Articles