If statements vs case

Hershel Fisch hershf at rgllc.us
Sun Feb 25 21:42:14 EST 2007


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
Hershel, Thanks

> 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.
> 
> But "c" and "d" both trigger "DoThing3", with "c" first triggering
> "DoThing2" before falling through to the next case.  It falls through
> because there is no "break" statement".   And because there is a "break"
> after "DoThing3", neither "c" nor "d" every triggers "DoThing4", since
> the "break" prevents the fall-through.
> 
> With the multiple criteria exemplified by "a" and "b" above, and the
> fall-through of having no break between "c" and "d", you can see that
> case blocks offer a level of flexibility not found in if-thens.
> 
> And for many uses, FWIW I often find case blocks easier to read.




More information about the use-livecode mailing list