Conditionals in switch 'case' labels

Discussion in 'Javascript' started by Mark Anderson, Feb 26, 2004.

  1. 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
     
    Mark Anderson, Feb 26, 2004
    #1
    1. Advertising

  2. > 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
     
    Douglas Crockford, Feb 26, 2004
    #2
    1. Advertising

  3. "Mark Anderson" <> writes:

    > 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
    --
    Lasse Reichstein Nielsen -
    DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
    'Faith without judgement merely degrades the spirit divine.'
     
    Lasse Reichstein Nielsen, Feb 26, 2004
    #3
  4. Douglas,

    Thanks very much.

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

    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
     
    Mark Anderson, Feb 26, 2004
    #4
  5. >>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
     
    Douglas Crockford, Feb 26, 2004
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Martin Magnusson

    Enumerations as case labels

    Martin Magnusson, Mar 16, 2005, in forum: C++
    Replies:
    3
    Views:
    451
    Mark A. Gibbs
    Mar 16, 2005
  2. Christopher Benson-Manica

    goto to switch labels

    Christopher Benson-Manica, Oct 4, 2004, in forum: C Programming
    Replies:
    7
    Views:
    349
    Mabden
    Oct 6, 2004
  3. case labels

    , Mar 28, 2008, in forum: C Programming
    Replies:
    67
    Views:
    1,713
    David Thompson
    Apr 7, 2008
  4. sandeep

    case labels

    sandeep, May 31, 2010, in forum: C Programming
    Replies:
    19
    Views:
    579
    Tim Rentsch
    Jun 18, 2010
  5. Randy Kramer
    Replies:
    11
    Views:
    387
    Randy Kramer
    Oct 19, 2007
Loading...

Share This Page