If statements vs case
Peter Alcibiades
palcibiades-first at yahoo.co.uk
Thu Mar 1 06:37:27 EST 2007
This is perfect - clear succinct and very useful, and exactly what naive users
mean when they find the documentation a little sketchy. Its having to work
out stuff like this by hand from the dictionary and then get it down in notes
someplace. Thanks.
But... why not get it into the online help as well?
Peter
On Monday 26 February 2007 02:31, Richard Gaskin wrote:
> 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