Conditionals in switch 'case' labels

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Mark Anderson

    #1

    Conditionals in switch 'case' labels

    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'||'Gr ouco'||'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


  • Douglas Crockford

    #2
    Re: Conditionals in switch 'case' labels

    > Is this sort of thing possible:[color=blue]
    >
    > var X = 'Moe';
    > switch (X) {
    > case 'Curly'||'Moe'| |'Larry':
    > alert('Found one of the Three Stooges');
    > case 'Chico'||'Harpo '||'Zeppo'||'Gr ouco'||'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.[/color]

    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.


    Comment

    • Lasse Reichstein Nielsen

      #3
      Re: Conditionals in switch 'case' labels

      "Mark Anderson" <mark@notmeyear dley.demon.co.u k> writes:
      [color=blue]
      > Is this sort of thing possible:
      >
      > var X = 'Moe';
      > switch (X) {
      > case 'Curly'||'Moe'| |'Larry':[/color]

      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.
      [color=blue]
      > This gives 'No matches' unless I only put a single string in the 'case'
      > lines.[/color]

      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 - lrn@hotpop.com
      DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleD OM.html>
      'Faith without judgement merely degrades the spirit divine.'

      Comment

      • Mark Anderson

        #4
        Re: Conditionals in switch 'case' labels

        Douglas,

        Thanks very much.
        [color=blue][color=green]
        >> JavaScript is not VBScript. You need to better educate yourself.[/color][/color]

        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


        Comment

        • Douglas Crockford

          #5
          Re: Conditionals in switch 'case' labels

          >>JavaScript is not VBScript. You need to better educate yourself.
          [color=blue]
          > 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?[/color]

          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.


          Comment

          Working...