If statements vs case
Richard Gaskin
ambassador at fourthworld.com
Sun Feb 25 21:31:42 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.
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.
--
Richard Gaskin
Managing Editor, revJournal
_______________________________________________________
Rev tips, tutorials and more: http://www.revJournal.com
More information about the use-livecode
mailing list