Velocity Reviews > Switch statement help

# Switch statement help

VK
Guest
Posts: n/a

 01-09-2006

Lee wrote:
> >case 10 :
> >case 20:
> >case 30:
> >default:
> >
> >If switch happened to be equal 10, then after the first case: it also
> >equals to 20 and 30 !

>
> As usual, you've done a fine job of convincing us that you don't
> doesn't tell us that the switch equals 10, 20, and 30, it tells
> us that 10, 20 and 30 are to be handled in the same way. It's
> just that sort of distinction that you seem to have trouble with.

Boy, that was just a short scheme to illustrate my point, not a code to
execute.
Sapienti sat - but if it's not the case then here a full scale code:

<script type="text/javascript">
var v = 10;
switch (v) {
case 10 : alert("v equals 10");
case 20 : alert("v equals 20");
case 30 : alert("v equals 30");
default: alert("v is out of range");
}
</script>

X l e c t r i c
Guest
Posts: n/a

 01-09-2006
Mark wrote:

"I have:
function payCalc(field, field2)
switch(field.value)
{
case 0-50: field2.value="lower band";
case 51-99: field2.value="mid band";
case 100-9999: field2.value="high band";
}"

Shouldn't the switch statement within the function also be enclosed in
curly brackets ? Like this:

function payCalc(field, field2)
{
switch(field.value)
{
case 0-50: field2.value="lower band";
case 51-99: field2.value="mid band";
case 100-9999: field2.value="high band";
}
}

In addition to using a break or return statement after each case, isn't
there also suppose to be a default case at the end ? Or is that not
necessary ?

Later, Art.

Lee
Guest
Posts: n/a

 01-09-2006
VK said:
>
>
>Lee wrote:
>> >case 10 :
>> >case 20:
>> >case 30:
>> >default:
>> >
>> >If switch happened to be equal 10, then after the first case: it also
>> >equals to 20 and 30 !

>>
>> As usual, you've done a fine job of convincing us that you don't
>> doesn't tell us that the switch equals 10, 20, and 30, it tells
>> us that 10, 20 and 30 are to be handled in the same way. It's
>> just that sort of distinction that you seem to have trouble with.

>
>Boy, that was just a short scheme to illustrate my point, not a code to
>execute.
>Sapienti sat - but if it's not the case then here a full scale code:
>
><script type="text/javascript">
>var v = 10;
>switch (v) {
> case 10 : alert("v equals 10");
> case 20 : alert("v equals 20");
> case 30 : alert("v equals 30");
> default: alert("v is out of range");
>}
></script>

I don't see your point. GIGO.
If you don't know how to use a construct, don't guess.

>

Evertjan.
Guest
Posts: n/a

 01-09-2006
X l e c t r i c wrote on 09 jan 2006 in comp.lang.javascript:

> case 0-50: field2.value="lower band";
> case 51-99: field2.value="mid band";
> case 100-9999: field2.value="high band";
>

Besides all that is said,
this means:

case -49: field2.value="lower band";
case -48: field2.value="mid band";
case -9899: field2.value="high band";

--
Evertjan.
The Netherlands.

Tim Streater
Guest
Posts: n/a

 01-09-2006
In article <(E-Mail Removed)>,
http://www.velocityreviews.com/forums/(E-Mail Removed) (X l e c t r i c) wrote:

> Mark wrote:
>
> "I have:
> function payCalc(field, field2)
> switch(field.value)
> {
> case 0-50: field2.value="lower band";
> case 51-99: field2.value="mid band";
> case 100-9999: field2.value="high band";
> }"
>
> Shouldn't the switch statement within the function also be enclosed in
> curly brackets ? Like this:
>
> function payCalc(field, field2)
> {
> switch(field.value)
> {
> case 0-50: field2.value="lower band";
> case 51-99: field2.value="mid band";
> case 100-9999: field2.value="high band";
> }
> }

I suppose so, strickly, tho I wouldn't lay it out like that. Of course
"case 0-50:" does not work.

> In addition to using a break or return statement after each case, isn't
> there also suppose to be a default case at the end ? Or is that not
> necessary ?

Only if you want one. If you leave it out and the value matches none of
the cases then the switch does nothing. This can be useful depending on
what you want.

Certainly the break should be required. That does allow you to do this
sort of thing:

switch (value)
{

case 1:
case 2:
x = 3;
break;

case 15:
x = 8;
break;

}

In fact in this example you don't need the send break, but it's best to
add it for the day when you add another case. Code that's gonna live
more than 15 mins should be written with a view to future maintenance.

-- tim

Lasse Reichstein Nielsen
Guest
Posts: n/a

 01-09-2006
"Julian Turner" <(E-Mail Removed)> writes:

> VK wrote:
>
> [snip]
>> P.P.S. Also remember a very annoying cross-language bug in switch
>> construct: it executes all underlaying branches unless you stop it with
>> "break" statement.

> Just interested in why you consider this to be a "bug"?

It's not a bug.

It's just a very annoying and error prone feature that was introduced
in C a long time ago, back when brevity was a bigger virtue than

> Further, leaving out the break statement can IMHO be useful sometimes
> (if for instance you want a given Statement to be executed for more
> than one case).

You could allow several cases for each branch, without needing
fallthrough between branches.

The few algorithms that can actually use fallthrough does not justify
the gazillion errors created by unwary programmers through the years.

> So perhaps rather than "bug", do you mean: "source of potential errors
> for the unwary"?

I don't speak for VK, but I mean that.

/L
--
Lasse Reichstein Nielsen - (E-Mail Removed)
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'

Julian Turner
Guest
Posts: n/a

 01-09-2006

Lasse Reichstein Nielsen wrote:

[snip]
> You could allow several cases for each branch, without needing
> fallthrough between branches.
>
> The few algorithms that can actually use fallthrough does not justify
> the gazillion errors created by unwary programmers through the years.

Fair point. I have found a few instances where fall-through was a
potentially useful option, but have tended to avoid it as in all cases
there were clearer alternative ways of doing the same thing.

Regards

Julian Turner

Tim Streater
Guest
Posts: n/a

 01-09-2006
In article <(E-Mail Removed)>,
Lasse Reichstein Nielsen <(E-Mail Removed)> wrote:

> "Julian Turner" <(E-Mail Removed)> writes:
>
> > VK wrote:
> >
> > [snip]
> >> P.P.S. Also remember a very annoying cross-language bug in switch
> >> construct: it executes all underlaying branches unless you stop it with
> >> "break" statement.

>
> > Just interested in why you consider this to be a "bug"?

>
> It's not a bug.
>
> It's just a very annoying and error prone feature that was introduced
> in C a long time ago, back when brevity was a bigger virtue than

It's not an annoying and error-prone feature. It's very useful. It adds
flexibility. I contrast this with Pascal, which had a number of shitty
irritations such as this (i.e., not needing the break as it was
inherently there) in the name of language "purity".

-- tim

Lee
Guest
Posts: n/a

 01-09-2006
Lasse Reichstein Nielsen said:

>The few algorithms that can actually use fallthrough does not justify
>the gazillion errors created by unwary programmers through the years.

It's a very handy way to identify people who don't know how to
read a manual. Programmers should avoid guessing how features
work.

Lee
Guest
Posts: n/a

 01-09-2006
X l e c t r i c said:
>
>Mark wrote:
>
>"I have:
>function payCalc(field, field2)
>switch(field.value)
>{
>case 0-50: field2.value="lower band";
>case 51-99: field2.value="mid band";
>case 100-9999: field2.value="high band";
>}"
>
>Shouldn't the switch statement within the function also be enclosed in
>curly brackets ? Like this:
>
>function payCalc(field, field2)
>{
>switch(field.value)
>{
>case 0-50: field2.value="lower band";
>case 51-99: field2.value="mid band";
>case 100-9999: field2.value="high band";
>}
>}
>
>In addition to using a break or return statement after each case, isn't
>there also suppose to be a default case at the end ? Or is that not
>necessary ?

RTFM