Another doubt when using switch

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Robert Scheer

    Another doubt when using switch

    Hi.

    I am trying to use switch this way, without success:

    //thedata is a numeric variable
    switch (thedata) {
    case < 10:
    ...
    case < 20:
    ...
    case <= 30:
    ...
    }

    It seems that switch can only test for single string values. Can't I
    make this kind of test using switch?

    Thanks,
    Robert Scheer
  • lallous

    #2
    Re: Another doubt when using switch

    Hello,

    You can't use 'switch' like that...you can check for exact values only, as:

    switch(value)
    {
    case 6:
    case 7:
    case 8:
    // do something
    break;
    case 100:
    // do something else...
    break;
    default:
    // default value...
    break;
    }

    If you need to compare for ranges rather than exact values then the 'if/else
    if' is what you need:

    if (x < 10) { ...... }
    else if (x > 30) { .....}
    else { .......}

    --
    Elias
    "Robert Scheer" <rbscheer@my-deja.com> wrote in message
    news:cfd22ab6.0 401160650.4defb 9e9@posting.goo gle.com...[color=blue]
    > Hi.
    >
    > I am trying to use switch this way, without success:
    >
    > //thedata is a numeric variable
    > switch (thedata) {
    > case < 10:
    > ...
    > case < 20:
    > ...
    > case <= 30:
    > ...
    > }
    >
    > It seems that switch can only test for single string values. Can't I
    > make this kind of test using switch?
    >
    > Thanks,
    > Robert Scheer[/color]


    Comment

    • Douglas Crockford

      #3
      Re: Another doubt when using switch

      > I am trying to use switch this way, without success:[color=blue]
      >
      > //thedata is a numeric variable
      > switch (thedata) {
      > case < 10:
      > ...
      > case < 20:
      > ...
      > case <= 30:
      > ...
      > }
      >
      > It seems that switch can only test for single string values. Can't I
      > make this kind of test using switch?[/color]

      Buy a book. You can't expect to program in total ignorance. Fortify yourself
      with specific knowledge.

      In this case, use an if statement.

      if (thedata < 10) {
      ...
      } else if (thedata < 20) {
      ...
      } else if (thedata <= 30) {
      ....
      }



      Comment

      • Michael Winter

        #4
        Re: Another doubt when using switch

        On Fri, 16 Jan 2004 07:42:39 -0800, Douglas Crockford <nospam@covad.n et>
        wrote:
        [color=blue]
        > Buy a book. You can't expect to program in total ignorance. Fortify
        > yourself with specific knowledge.[/color]

        Even use of the JavaScript language reference would do...

        Mike



        --
        Michael Winter
        M.Winter@blueyo nder.co.invalid (replace ".invalid" with ".uk" to reply)

        Comment

        • Dr John Stockton

          #5
          Re: Another doubt when using switch

          JRS: In article <bu8v0h$f4s0h$1 @ID-161723.news.uni-berlin.de>, seen in
          news:comp.lang. javascript, lallous <lallous@lgwm.o rg> posted at Fri, 16
          Jan 2004 17:05:59 :-[color=blue]
          >"Robert Scheer" <rbscheer@my-deja.com> wrote in message
          >news:cfd22ab6. 0401160650.4def b9e9@posting.go ogle.com...[/color]
          [color=blue][color=green]
          >> I am trying to use switch this way, without success:
          >>
          >> //thedata is a numeric variable
          >> switch (thedata) {
          >> case < 10:
          >> ...
          >> case < 20:
          >> ...
          >> case <= 30:
          >> ...
          >> }
          >>
          >> It seems that switch can only test for single string values. Can't I
          >> make this kind of test using switch?[/color][/color]

          You can : see below previous response.


          Responses should go after trimmed quotes, as per Wednesday FAQ.
          [color=blue]
          >You can't use 'switch' like that...you can check for exact values only, as:[/color]

          Well, yes and no.

          [color=blue]
          >If you need to compare for ranges rather than exact values then the 'if/else
          >if' is what you need:
          >
          >if (x < 10) { ...... }
          >else if (x > 30) { .....}
          >else { .......}[/color]

          No. It can be used, but it is not needed.

          var X = 265 // the variable
          switch(true) {
          case X<10 : alert('<10') ; break
          case X<20 : alert('<20') ; break
          case X<30 : alert('<30') ; break
          default : alert('big') }


          A post here or elsewhere a few days/weeks ago provided a clue from which
          the above is derived. The code appears to be about as long as that with
          nested IF; appears legal; can easily be laid out readably; works (at
          least in eval in MSIE4); and ISTM that it should be of similar speed.
          Advantage: case & break fulfil the role of brackets; more typing, but
          easier to pair them up correctly.

          To confuse readers : change true to false and reverse the relational
          operators ( < to >= ).

          --
          © John Stockton, Surrey, UK. ?@merlyn.demon. co.uk Turnpike v4.00 IE 4 ©
          <URL:http://jibbering.com/faq/> Jim Ley's FAQ for news:comp.lang. javascript
          <URL:http://www.merlyn.demo n.co.uk/js-index.htm> jscr maths, dates, sources.
          <URL:http://www.merlyn.demo n.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.

          Comment

          Working...