How do you craft a switch statement in as3 to make the case apply to an 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've tried multiple ways to make the case apply for all numbers between 10-100, and 110-1000, but can't seem to find a way to do it, and I can't find the proper syntax for such a thing in as3.
Using range in switch case in C/C++ In the switch statement we pass some value, and using different cases, we can check the value. Here we will see that we can use ranges in the case statement. After writing case, we have to put lower value, then one space, then three dots, then another space, and the higher value.
You can decide which to use, based on readability and other factors. An if statement can be used to make decisions based on ranges of values or conditions, whereas a switch statement can make decisions based only on a single integer or enumerated value.
Some Important Rules for Switch Statements The value for a case must be of the same data type as the variable in the switch. The value for a case must be constant or literal. Variables are not allowed. The break statement is used inside the switch to terminate a statement sequence.
You can use a 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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With