[thelist] Switch statement

Jay Blanchard jay.blanchard at niicommunications.com
Fri Jul 19 07:40:00 CDT 2002


[snip]
switch (foo){
	case 'blah':
		doSomething();
		break;
	case 'bleh':
		doSomeOtherThing();
		break;
}

Is 'break' really needed? I think that when the first condition ("blah") is
satisfied, the second one will never happen. Shouldn't it be this way?

I'm asking that because when I forgot to use 'break' I had a lot of
problems. The browser executed the statements for the first condition and
also the others.
[/snip]

Seems you have answered your own question! :^] Break is needed, not only in
JavaScript but other languages as well. Once 'something is done in one case'
the next case may or may not be aware of it...so the code continues. Break
is the same as exit for any conditional where each condirional is not
'aware' of the next set of rules. Not to mention that it is good coding
style. This is different behavior from an 'if', and so may seem a little
counter-intuitive.

HTH!

Jay

"The witch of the west was framed "

*************************************
* Want to meet other PHP developers *
* in your area? Check out:          *
* http://php.meetup.com/            *
* No developer is an island ...     *
*************************************





More information about the thelist mailing list