Switch versus if/then/else ( was: Main menu puzzle, Klaus)

Garrett Hylltun garrett at paraboliclogic.com
Sat Feb 18 18:17:30 EST 2006


On Feb 18, 2006, at 2:37 PM, Eric Chatonet wrote:

> switch
>   case x < 100
>   case  x > 500
>   case  x > 200 and x < 400
>     <statements>
>     break
>   default
>     <other statements>
> end switch
>
> With a conditional structure you would write:
>
> if x < 100 or x > 500 or (x > 200 and x < 400) then
>   <statements>
> else
>  <other statements>
> end if
>
> The first formulation seems much more clear to me :-)

To me no.  The if statement seems all too clear.  I think this  
greatly depends on what past experiences a programmer may have had.

I spent years programming in Visual DialogScript on Windows, and  
there were no switch/case in the syntax (It's an odd syntax in the  
first place).  The above would look like this:

If @not(@greater(%x,100))@greater(%x,500)@both(@greater(%x,200), at not 
(@greater(%x,400)))
   <statements>
else
   <statements>
end

So you can see how one could become rather used to a certain flow or  
way of things.  It becomes second nature.  I haven't programmed in  
Visual DialogScript since last summer and still instinctively I was  
able to toss together the if structure there.  And of course, it  
carries over when moving to another language.  That and one other  
thing, is that you can always count on another language having "IF/ 
ELSE/END" in it's syntax, so it's kind of a universal attribute for  
programming languages.

Not that the switch/case isn't more logical or vice versa.

-Garrett




More information about the use-livecode mailing list