Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Switch statements and ranges of numbers

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.

like image 915
Cap'nAhab Avatar asked Apr 30 '11 06:04

Cap'nAhab


People also ask

Can you do ranges in switch statements?

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.

Do switch statements work with integers?

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.

What are the rules for switch statement?

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.


1 Answers

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
like image 60
OXMO456 Avatar answered Dec 24 '22 15:12

OXMO456