Conditionals in switch 'case' labels

M

Mark Anderson

Is this sort of thing possible:

var X = 'Moe';
switch (X) {
case 'Curly'||'Moe'||'Larry':
alert('Found one of the Three Stooges');
case 'Chico'||'Harpo'||'Zeppo'||'Grouco'||'Gummo':
alert('Found one of the Marx Brothers');
default:
alert('No matches');

This gives 'No matches' unless I only put a single string in the 'case'
lines. I've just been using VB's Select Case which is a similar flow
control but which allows conditional arguments in the 'cases'. I just
wondered...

I realise you could put each set of names in an array and iterate
through each array, but that's a different issue.

Regards

Mark Anderson
 
D

Douglas Crockford

Is this sort of thing possible:
var X = 'Moe';
switch (X) {
case 'Curly'||'Moe'||'Larry':
alert('Found one of the Three Stooges');
case 'Chico'||'Harpo'||'Zeppo'||'Grouco'||'Gummo':
alert('Found one of the Marx Brothers');
default:
alert('No matches');

This gives 'No matches' unless I only put a single string in the 'case'
lines. I've just been using VB's Select Case which is a similar flow
control but which allows conditional arguments in the 'cases'. I just
wondered...

I realise you could put each set of names in an array and iterate
through each array, but that's a different issue.

var X = 'Moe';
switch (X) {
case 'Curly':
case 'Moe':
case 'Larry':
alert('Found one of the Three Stooges');
break;
case 'Chico':
case 'Harpo':
case 'Zeppo':
case 'Groucho':
case 'Gummo':
alert('Found one of the Marx Brothers');
break;
default:
alert('No matches');
}

You could also put them in an object:

var name = {
Curly: 'Three Stooges',
Moe: 'Three Stooges',
Larry: 'Three Stooges',
Chico: 'Marx Brothers',
Harpo: 'Marx Brothers',
Zeppo: 'Marx Brothers',
Chico: 'Marx Brothers',
Groucho: 'Marx Brothers'}

var n = name[X];
if (n) {
alert('Found one of the ' + n);
} else {
alert('No matches');
}

JavaScript is not VBScript. You need to better educate yourself.

http://www.crockford.com/#javascript
 
L

Lasse Reichstein Nielsen

Mark Anderson said:
Is this sort of thing possible:

var X = 'Moe';
switch (X) {
case 'Curly'||'Moe'||'Larry':

The syntax is the traditional C syntax, so you can write:
case 'Curly': case 'Moe': case 'Larry':
alert('Found one of the Three Stooges');
break;

Remember the break, or the following code will also be executed.
This gives 'No matches' unless I only put a single string in the 'case'
lines.

It would actually match the first option. Try with 'Curly'.
The reason is that Javascript allows expressions in the case construct.
It first evaluates
'Curly'||'Moe'||'Larry'
The value of that is 'Curly'. I then tests that value against the
switch value, so 'Curly' would be matched.

/L
 
M

Mark Anderson

Douglas,

Thanks very much.

Ouch - I'm not sure what I said to warrant such a put down. Seeing as
this fact isn't in the FAQ and not in the basic 'switch' usage examples
I found online, is the question not valid in this newsgroup without
attracting such contempt?

FWIW, Visual Basic - to which I referred - is *not* VBScript. Indeed, it
is a pale shadow of the former but I'm here to say thanks for the info
supplied not for a newsgroup fight; I simply wondered whether a useful
method available in language A had similar offering in language B - I
wasn't implying, lest I was misunderstood, any comparative value
judgement. As it happens, my train of thought started was because I was
working in AppleScript (if I can mention that name here <g>) and wanted
a 'switch' type construct - which, as it happens, AS doesn't have.
Chopping between languages it's hard to remember the constructs of each
one - they tend to blur.

On a more positive note, thank you kindly for your examples. The object
approach is new to me and one I'll certainly look into further.

Regards

Mark
 
D

Douglas Crockford

JavaScript is not VBScript. You need to better educate yourself.
Ouch - I'm not sure what I said to warrant such a put down. Seeing as
this fact isn't in the FAQ and not in the basic 'switch' usage examples
I found online, is the question not valid in this newsgroup without
attracting such contempt?

It was not contempt. It was good advice. In my opinion, browser
scripting is the most difficult form of programming because of the great
need for portability and the extremely poor quality and inconsistency of
the browsers. Rigor and knowledge are extremely important here.

You should not attempt to program in any language without a good
reference at hand.

http://www.crockford.com
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top