Re: Is there limitation for switch case: argument's value?

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Aargh. Slipped on the trigger there--premature Send. See below for
what I meant to send:

2009/8/22 Lars Torben Wilson <torben@xxxxxxx>:
> 2009/8/22 Keith <survivor_bus@xxxxxxxxxxx>:
>> Thanks! Torben.
>> I got the point now and it works! :-)
>> I'm doing this because the statements of each cases is quite long, and I
>> wish to have minimum coding without repetition.

Hi Keith,

Glad it works! I'm not sure how inverting the case statement helps you
minimize the code in each case. As both I and Adam showed, you can do
the same thing more efficiently (and IMHO more readably) like
this:

switch ($sum)
{
case 8:
    echo "The sum is 8";
    break;
case 7:
case 6:
    echo "The sum is 7 or 6";
    break;
case 2:
case 1:
    echo "The sum is 2 or 1";
    break;
case 0:
    echo "The sum is 0";
    break;
default:
    echo "The sum is 3, 4, or 5";
    break;
}

And if the code is getting so long that it's getting unwieldy, you
might want to look into breaking it out into separate functions.

Anyway, it's not a huge thing; personally though I feel that it's a
syntax that if ever used, should only be used for very good reasons.
Most of the uses I've seen of it, however, fall into the "overly
clever" category and add nothing in the end to the quality of the
code. That's totally a judgement call on my part, of course. :)

Again, glad it works, and keep on coding!


Regards,

Torben

>> "Lars Torben Wilson" <larstorben@xxxxxxxxx> wrote in message
>> news:36d4833b0908202323p3c858b5fn6a1d6775aa7f8997@xxxxxxxxxxxxxxxxx
>>>
>>> 2009/8/20 Keith <survivor_bus@xxxxxxxxxxx>:
>>>>
>>>> Hi,
>>>> I encounter a funny limitation here with switch case as below:
>>>> The value for $sum is worked as expected for 1 to 8, but not for 0.
>>>> When the $sum=0, the first case will be return, which is "sum=8".
>>>> Is there any limitation / rules for switch case? Thanks for advice!
>>>>
>>>> Keith
>>>
>>> Hi Keith,
>>>
>>> Try replacing 'switch($sum)' with 'switch(true)'.
>>>
>>> Note that unless you have very good reasons for using a switch
>>> statement like this, and know exactly why you're doing it, it's often
>>> better just to use it in the normal fashion. i.e.:
>>>
>>>      switch ($sum)
>>>       {
>>>       case 8:
>>>           break;
>>>       case 7:
>>>       case 6:
>>>           break;
>>>       case 2:
>>>       case 1:
>>>           break;
>>>       case 0:
>>>           break;
>>> default:
>>>           break;
>>>       }
>>>
>>> Some people like the syntax you've presented but honestly, there's
>>> usually a better way to do it.
>>>
>>> This is also somewhat faster too, although you may only notice the
>>> difference in very tight loops where you're counting every nanosecond.
>>>
>>>
>>> Regards,
>>>
>>> Torben
>>>
>>>> $sum=0;
>>>> switch($sum)
>>>> {
>>>>  case ($sum==8):
>>>>  echo "sum=8";
>>>>  break;
>>>>      case ($sum==7 || $sum==6):
>>>>      echo "sum=7 or 6";
>>>>      break;
>>>>  case ($sum==2 || $sum==1):
>>>>  echo "sum=2 or 1";
>>>>  break;
>>>>      case 0:
>>>>      echo "sum=0";
>>>>      break;
>>>>  default:
>>>>  echo "sum=3/4/5";
>>>>  break;
>>>> }
>>>> --
>>>> PHP General Mailing List (http://www.php.net/)
>>>> To unsubscribe, visit: http://www.php.net/unsub.php
>>>>
>>>>
>>
>> --
>> PHP General Mailing List (http://www.php.net/)
>> To unsubscribe, visit: http://www.php.net/unsub.php
>>
>>
>

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux