Switch & Case unclear to me
#2

pawn Код:
if(a == 7 || a == 8)

//equals to:
switch (a)
{
    case 7, 8: {
    ///Something
    }
}
It can shorten at some cases. For example, if you want only specific numbers.
pawn Код:
if(a == 7 || a == 31 || a == 98 || a == 104 || a == 1009)
{
    //stuff
}

//Equals to
switch(a) {
    case 7, 31, 98, 104, 1009: {
        //Stuff
    }
}
Or just for a range of values... Lets say... 1 to 99:
pawn Код:
if(a >= 1 && a <= 99)
{
     //Something
}
else if(a >= 100 && a <= 199)
{
    //Something
}
else if(a >= 200 && a <= 299)
{
    //Something
}
else
{
    //Something
}
Just like doing:
pawn Код:
switch (a)
{
    case 1...99:
    {
    }
    case 100...199:
    {
    }
    case 200...299:
    {
    }
    default: //Default equals to 'else' - when no other option is available, default is called.
    {
    }
}
So yeah, if you know when to use it, it can save tons of space.
Reply


Messages In This Thread
Switch & Case unclear to me - by arjanforgames - 08.08.2013, 20:30
Re: Switch & Case unclear to me - by [XST]O_x - 08.08.2013, 20:40
Re: Switch & Case unclear to me - by Konstantinos - 08.08.2013, 20:43
Re: Switch & Case unclear to me - by arjanforgames - 09.08.2013, 06:03
Re: Switch & Case unclear to me - by Scenario - 09.08.2013, 06:11
Re: Switch & Case unclear to me - by Vince - 09.08.2013, 06:18
Re: Switch & Case unclear to me - by Misiur - 09.08.2013, 06:20
Re: Switch & Case unclear to me - by Scenario - 09.08.2013, 06:46

Forum Jump:


Users browsing this thread: 1 Guest(s)