[thelist] Switch statement

Lachlan Cannon luminosity at members.evolt.org
Fri Jul 19 07:41:00 CDT 2002


Amazon Paulo wrote:
> Hi,
>
> I've been using switch statement (javascript)
> 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?

Well, I think the point is that you can use the break either way you
want. Most people will just use the statements for that case, and they
only have to add a break; to the end of each one, while people who need
to be able to sequentially execute the statements can do that without
needing a heap more performance-reudicng code.

> 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.

<tip type="writing code" author="Lachlan">
When writing code that has an end and start point always write the end
part when you write the start. You won't get confused where it's meant
to go in long code, since it's already there, and you won't forget to
put it there.

For instance when writing an if statement write if($var = 'cat') {

}

And then go and put the code in the middle, or when writing a switch write

case 'evolt':

break;

And then go back in to write the code.
</tip>

--
Lach
__________________________________________
http://members.evolt.org/luminosity/
MSN: luminosity @ members.evolt.org
__________________________________________





More information about the thelist mailing list