Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > Switch as a Select Case

Reply
Thread Tools

Switch as a Select Case

 
 
Robert Scheer
Guest
Posts: n/a
 
      01-06-2004
Hi.

In VBScript I can use a Select Case statement like that:

Select Case X
Case 1 to 10 'X is between 1 and 10

Case 11,14,16 'X is 11 or 14 or 16
End Select

Is it possible to use the switch statement in Javascript to do that?
Do I have to use an if?

Thanks,
Robert Scheer
 
Reply With Quote
 
 
 
 
McKirahan
Guest
Posts: n/a
 
      01-06-2004
"Robert Scheer" <> wrote in message
news: om...
> Hi.
>
> In VBScript I can use a Select Case statement like that:
>
> Select Case X
> Case 1 to 10 'X is between 1 and 10
>
> Case 11,14,16 'X is 11 or 14 or 16
> End Select
>
> Is it possible to use the switch statement in Javascript to do that?
> Do I have to use an if?
>
> Thanks,
> Robert Scheer


Yes; "switch" is to JS as "select" is to VBS.
To exit a "case", add "break".


Description

Enables the execution of one or more statements when a specified
expression's value matches a label.

Syntax

switch (expression) {
case label :
statementlist
case label :
statementlist
...
default :
statementlist
}


 
Reply With Quote
 
 
 
 
Christopher Jeris
Guest
Posts: n/a
 
      01-06-2004
(Robert Scheer) writes:
> In VBScript I can use a Select Case statement like that:
> Select Case X
> Case 1 to 10 'X is between 1 and 10
> Case 11,14,16 'X is 11 or 14 or 16
> End Select


JavaScript's syntax is based on that of C, and as in C,
there is no syntactic sugar for switch statements. However,
as in C, omitting the "break;" after a switch case will cause
execution to fall through to the following case. (Opinion is
divided on whether this is a good thing.)

switch (x) {
case 1:
case 2:
case 3:
/* ... */
case 10:
// execute some code for this case
break;

case 11:
case 14:
case 16:
// execute some other code for this thing
break;

default:
// complain
break; // for uniformity
}

In my opinion it is good practice to mark with LOUD COMMENTS
places where you use this "fall-through" feature -- except for
code like the example above, where it's fairly obvious from the
long string of "case n:" without any intervening code.

switch (x) {
case 1:
// some stuff that has to happen in case 1
/* *** FALL THROUGH *** */
case 2:
// some stuff that has to happen either for 1 or 2
break;
}

In a case such as the VB example you gave above, a chained if-else
might in fact be clearer and less verbose:

if (1 <= x && x <= 10) {
// do some stuff
} else if (x == 11 || x == 14 || x == 16) {
// do some other stuff
} else {
// complain
}

--
Chris Jeris Apply (1 6 2 4)(3 7) to domain to reply.

 
Reply With Quote
 
Keith Bowes
Guest
Posts: n/a
 
      01-06-2004
Christopher Jeris wrote:
>
> JavaScript's syntax is based on that of C, and as in C,
> there is no syntactic sugar for switch statements.


I disagree. The convenient syntaxes used in BASIC, Pascal, etc. existed
before C was a language. C was designed to be very low-level, where
efficiency is paramount; JavaScript is an interpreted language for web
pages and is well known to be inefficient, and therefore there is no
reason for stodgily keeping such inconveniences. Furthermore, GCC's
version of C has the ability for ranges, with a syntax very similar to
Pascal's.

 
Reply With Quote
 
rh
Guest
Posts: n/a
 
      01-06-2004
Christopher Jeris <> wrote in message news:<>...
> (Robert Scheer) writes:
> > In VBScript I can use a Select Case statement like that:
> > Select Case X
> > Case 1 to 10 'X is between 1 and 10
> > Case 11,14,16 'X is 11 or 14 or 16
> > End Select

>


<...>

> In a case such as the VB example you gave above, a chained if-else
> might in fact be clearer and less verbose:
>
> if (1 <= x && x <= 10) {
> // do some stuff
> } else if (x == 11 || x == 14 || x == 16) {
> // do some other stuff
> } else {
> // complain
> }


Alternatively, if circumstance or preference implores a switch/case structure:

switch (x) {
case ( ( 1 <= x && x <= 10 ) ? x : x+1 ):
alert("case 1-10: " + x);
break;

case (11):
case (14):
case (16):
alert("case 11,14, or 16: " + x);
break;

default:
alert("None of the above: " + x);
}

.../rh
 
Reply With Quote
 
Lasse Reichstein Nielsen
Guest
Posts: n/a
 
      01-07-2004
Keith Bowes <> writes:

> Christopher Jeris wrote:
>> JavaScript's syntax is based on that of C, and as in C,
>> there is no syntactic sugar for switch statements.

>
> I disagree.


With what? That the syntax is based on that of C (it is, although
indirectly through the syntax of Java), or that there is no syntactic
sugar for switch statements (there almost isn't - except that the
values of a case can be arbitrary expressions)?

What you seem to be disagreeing with, is that it should be like that
(and I agree). For a scripting language, the options are too restrictive,
which is probably why we so rarely see switches.

/L
--
Lasse Reichstein Nielsen -
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
 
Reply With Quote
 
Christopher Jeris
Guest
Posts: n/a
 
      01-07-2004
(rh) writes:
> Alternatively, if circumstance or preference implores a switch/case
> structure:
> switch (x) {
> case ( ( 1 <= x && x <= 10 ) ? x : x+1 ):
> alert("case 1-10: " + x);
> break;
> case (11):
> case (14):
> case (16):
> alert("case 11,14, or 16: " + x);
> break;
> default:
> alert("None of the above: " + x);
> }


Clever. I knew that in JavaScript the controlling expression of a
switch does not have to have integer type (as in C) but did not
realize that it does not have to be a constant expression. I guess
when you don't want to compile your switch statement into a jump
table, it's not important that the labels be integer literals

However, this idiom is IMHO overly clever. It takes a few
moments to see that it really does the right thing.

--
Chris Jeris Apply (1 6 2 4)(3 7) to domain to reply.

 
Reply With Quote
 
rh
Guest
Posts: n/a
 
      01-07-2004
Christopher Jeris <> wrote in message news:<>...
> (rh) writes:
> > Alternatively, if circumstance or preference implores a switch/case
> > structure:
> > switch (x) {
> > case ( ( 1 <= x && x <= 10 ) ? x : x+1 ):
> > alert("case 1-10: " + x);
> > break;
> > case (11):
> > case (14):
> > case (16):
> > alert("case 11,14, or 16: " + x);
> > break;
> > default:
> > alert("None of the above: " + x);
> > }

>
> Clever. I knew that in JavaScript the controlling expression of a
> switch does not have to have integer type (as in C) but did not
> realize that it does not have to be a constant expression. I guess
> when you don't want to compile your switch statement into a jump
> table, it's not important that the labels be integer literals


The common "label" description doesn't really lead one to realize that
and expression can be used. That's why I thought it might be
worthwhile to provide an example.

Whether a particular construct meets efficiency and other coding
requirements is a decision that's up to the (hopefully well-informed)
author.

>
> However, this idiom is IMHO overly clever. It takes a few
> moments to see that it really does the right thing.


The following Javascript statement:

x = x || 1;

would likely take a few moments for the uninitiated to determine what
it does, let alone whether it does the "right thing". I don't believe
that stands in the way of it being fully utilized by those familiar
and comfortable with the fundamentals of the language.

../rh
 
Reply With Quote
 
rh
Guest
Posts: n/a
 
      01-07-2004
Lasse Reichstein Nielsen <> wrote in message news:<>...
> Keith Bowes <> writes:
>
> > Christopher Jeris wrote:
> >> JavaScript's syntax is based on that of C, and as in C,
> >> there is no syntactic sugar for switch statements.

> >
> > I disagree.

>
> With what? That the syntax is based on that of C (it is, although
> indirectly through the syntax of Java), or that there is no syntactic
> sugar for switch statements (there almost isn't - except that the
> values of a case can be arbitrary expressions)?
>
> What you seem to be disagreeing with, is that it should be like that
> (and I agree). For a scripting language, the options are too restrictive,
> which is probably why we so rarely see switches.
>


Then again, at the other extreme, some scripting languages don't have
a switch construct -- e.g., Perl.

I'm not sure of the reason that switch isn't more commonly used in
Javascript. In the absence of an IDE editor, they can be a little bit
awkward to construct, and that wouldn't change a whole lot even if
"range sugar" was available.

A major advantage of switch is that it provides encapusulation and
clear delineation of the set of alternatives under current
consideration. That's something that can get obscured to some degree,
even with with well-formatted block structure, in code that uses
if/else exclusively to process conditionals.

../rh

> /L

 
Reply With Quote
 
Christopher Jeris
Guest
Posts: n/a
 
      01-07-2004
(rh) writes:
> A major advantage of switch is that it provides encapusulation and
> clear delineation of the set of alternatives under current
> consideration. That's something that can get obscured to some degree,
> even with with well-formatted block structure, in code that uses
> if/else exclusively to process conditionals.


\begin{sententious}
Since the correct programming language has already exhibited the
correct control structure to handle multiple alternatives, any
variations on it are at best superfluous. I refer of course to
the 'cond' of the Lisp family, which expresses a sequence of
if-else decisions symmetrically:

(cond ((<= 1 x 10) (do-something-with x))
((or (= x 11)
(= x 14)
(= x 16)) (do-something-else-with x))
(t (complain)))

\end{sententious} %

--
Chris Jeris Apply (1 6 2 4)(3 7) to domain to reply.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
switch or select case and code inside it richard Javascript 7 11-06-2008 09:12 AM
Dim inside Select case executes in any case? aa ASP General 14 04-24-2007 06:24 AM
"Case 1 To 5" in Select case, giving Error!!. Why? Lakshmi Narayanan.R ASP General 10 03-04-2005 01:13 AM
how to case select with case-insensitive string ? Tee ASP .Net 3 06-23-2004 07:40 PM
Select Case vs switch Tony MCAD 3 02-04-2004 05:59 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57