Log In
Sign Up and Get Started Blogging!
JoeUser is completely free to use! By Signing Up on JoeUser, you can create your own blog and participate on the blogs of others!
Vatsal's weblog....
Switch The "switch ()"
Published on May 16, 2005 By
LifeIs42
In
Software Development
Why does Java and other popular programming languages like C, C++ "Switch" statements have a fall through behaviour by default,
the most common or popular use of a switch statment is when we just want to choose a paticular option based on the value passed to switch statement and then get out of it, the fallthrough behavior is not what is needed most of the time ( atleast thats what my programming experience says..), you end up putting "break;" statements just to avoid this fall through most of times,
why was the switch statement not designed the other way round, put a statement in "case 'x':" which allows you to fallthrough if you need it, the default behavior will be just to "break" out of switch after a matching case is found,
will it not help and save lots of LOC worldwide...
Free Website Counter
Tracker
Popular Articles in this Category
Popular Articles from LifeIs42
Comments
1
verbat
on May 16, 2005
it is because of an error, and because people are hard to fix errors when they come from the tradition. Legacy knowledge sux.
Anyway, IIRC In c#, this works as expected, withouth fallthrough. Probably because C# is also a descendent of Delphi (and thus Pascal).
I prefer languages where the switch/case is more of a pattern match operation, i.e. ruby or perl with the switch module.
2
Tony Marston
on May 16, 2005
I had the same problem when I first started using the switch() statement in PHP after using it in a different language for 10 years, but after a while I began to see the advantages of allowing a fall-through with multiple case statements, and I have taken advantage of this on more than one occasion. Personally I prefer the choice the fall-through gives me - if I don't want a fall-through I simply insert a break statement, but if I DO want a fall-through, then I leave it out.
It's a similar case with the parent constructor when I instantiate a subclass - the parent constructor is not called by default. If I want it executed then I must do it explicitly. This is how I like it because most of the time I have code in the subclass which I want processed INSTEAD OF the code in the parent class. If the parent constructor were to be executed EVEN WHEN I DID NOT WANT IT TO then I would be snafu'd.
I like the flexibility that the switch fall-through and non-execution of the parent constructor give me. It means that I have complete control over what happens instead of the language making the wrong decision for me.
3
thomassen
on May 16, 2005
hmm... it says the article got 2 comments, but I can't see any.
Anyways,...
I'm not sure, but might it be because VB you can put several options separated by a comma,
Case 1, 2, 3:
//Do something
Case 4
however in JScript (haven't tried C or Java) you have to do something like
Case 1
Case 2
Case 3
//Do something
break;
Case 4
but that doesn't explain why JScript don't have comma separated switch list.
4
LifeIs42
on May 17, 2005
>>>
"switch() statement in PHP after using it in a different language for 10 years"
just plain curiosity...which language had this facility (of non fall through behavior)10 years ago...i thought this fallthrough is coming from olden days....
5
ijcd
on Jun 07, 2005
Because initially, in C, the switch() is just a shortcut for a jump based on value. Imagine your case statements as a list of instructions. The normal behavior after executing an operation is to move to the next operation. The case: labels are really just that, labels for points in the code sequence.
You want switch() to be more of a code construct, with an implicit JMP to the end of the block after your case: stanzas. That's a higher level view, but in C at least, switch is just a slightly higher level view of the machine code.
Welcome Guest! Please take the time to register with us.
There are many great features available to you once you register, including:
Richer content, access to many features that are disabled for guests like commenting on the forums.
Access to a great community, with a massive database of many, many areas of interest.
Access to contests & subscription offers like exclusive emails.
It's simple, and FREE!
Sign Up Now!
Meta
Views
» 9932
Comments
»
5
Category
»
Software Development
Comment
Recent Article Comments
Modding Ara: History Untold
LightStar Design Windowblind...
DeskScapes 11: The Dream Mak...
Which A.I. Software Are You ...
ChatGPT 4o vs. o1 vs. o1 Pro...
What's the Last Book You Rea...
A day in the Life of Odditie...
Let's start a New Jammin Thr...
Let's see your political mem...
Safe and free software downl...
Sponsored Links