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

James Spencer jspencer78 at mac.com
Sun Feb 19 12:17:45 EST 2006


On Feb 19, 2006, at 10:10 AM, Glenn E. Fisher wrote:

> As a historical note, (I'm a very old programmer since 1962) back  
> when I was writing compilers and emulators in B and C in the line  
> editing mode ;-), the switch case statements were really needed.   
> In these kinds of programs many many cases was the norm and the  
> nested if..then..else just didn't cut it.  So it depends on the  
> scope of the problem which is the best.

I can only claim to go back to '71 with Basic on a PDP-8 but let me  
say amen.  It can be shown that switch statements are not "necessary"  
in the sense that you can always simulate a switch with an if/else  
construct but if the options are based on a single value which has  
more than two possible values, a switch is MUCH less subject to error  
than is if/else if/else if.  It's not a strong argument against its  
use that beginners have trouble understanding the concept (although  
I'm not understanding what is so difficult about the concept).   
Beginners have terrible time getting the concepts of object oriented  
programming or exception handling too but these concepts have  
certainly resulted in much clearer, more maintainable code.

BTW, IMHO the conversation about whether this is how we think in  
everyday life is not very helpful: we don't think with the binary  
logic of computer programs at all.  E.g. we generally do not have a  
rigid list of who it might be at the door with a predetermined  
intention to take particular action depending on who it is.  This  
kind of rigidity, however, is part and parcel of computer programming  
whether it be xTalk or C or Basic.

James P. Spencer
Rochester, MN

jspencer78 at mac.com

"Badges??  We don't need no stinkin badges!"




More information about the use-livecode mailing list