If statements vs case

Richard Gaskin ambassador at fourthworld.com
Sun Feb 25 22:03:30 EST 2007


Hershel Fisch wrote:
>>> Hi every one, how would one put the differences between multiple if, else
>>> if's vs. case's
>> 
>> In many respects they're quite similar, and for many uses the choice of
>> one over another can be a matter of stylistic preference.
>> 
>> But there is at least one functional difference which may be worth
>> keeping in mind; I don't use it often, but I'm grateful for it when I do:
>> 
>> Case statements allow a fall-through option, so that each case need not
>> be exclusive the way if-then is.
>> 
>> For example, in this block:
>> 
>> switch tVar
>>  case "a"
>>  case "b"
>>    DoThing1
>>    break
>>  case "c"
>>    DoThing2
>>  case "d"
>>    DoThing3
>>    break
>>  case "e"
>>    DoThing4
>> end switch
>> 
>> ..the cases "a" and "b" both trigger "DoThing1", and the hit the break
>> so they exit.
>> 
> On 2/25/07 9:31 PM, "Richard Gaskin" <ambassador at fourthworld.com> wrote:
> Wouldn't be the same as
> If tVar ="a" or tVar ="b" then
>    DoThing1
>   Else
>    If tVar = "c" then
>      DoTing2
>    Else
>    If tVar ="d" then
>     DoTing4
>    Enf if
>   End if
> End if

Good point.  I suppose it may well be as simple as Jim put it, that the 
difference is stylistic.  But FWIW, I find the case example above much 
quicker to skim to grasp the logic than the if-then example below it.  I 
don't know if the literature supports that anecdotal observation, but 
since most languages include case there must be good value in it.

-- 
  Richard Gaskin
  Managing Editor, revJournal
  _______________________________________________________
  Rev tips, tutorials and more: http://www.revJournal.com



More information about the use-livecode mailing list