Switch..Case Statement Question.

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

    #1

    Switch..Case Statement Question.

    Can someone tell me if the following Switch...Case construct is valid?
    I'm wanting to check for multiple values in the Case statement without
    explicitly listing each values.
    So for example, will case 1-35: work? If this would work, will it
    consider all numbers between 1 and 35 inclusive of 1 and 35? Please let
    me know if the following will work below:

    switch(state){
    case 1-35:
    case 37:
    do something_1;
    break;
    case 36:
    do something_2;
    break;
    case 38-50:
    do something_3;
    break;
    }


    Thank you.
    Andy.

  • Evertjan.

    #2
    Re: Switch..Case Statement Question.

    Andy wrote on 28 feb 2005 in comp.lang.javas cript:
    [color=blue]
    > Can someone tell me if the following Switch...Case construct is valid?
    > I'm wanting to check for multiple values in the Case statement without
    > explicitly listing each values.
    > So for example, will case 1-35: work? If this would work, will it
    > consider all numbers between 1 and 35 inclusive of 1 and 35? Please let
    > me know if the following will work below:[/color]

    did you test it yourself? If not why not?
    [color=blue]
    > switch(state){
    > case 1-35:
    > case 37:
    > do something_1;
    > break;
    >[/color]

    this part will execute if "state" is -34 or 37,
    because 1-35 is 34, I think.

    But please test it yourself.


    --
    Evertjan.
    The Netherlands.
    (Replace all crosses with dots in my emailaddress)

    Comment

    • Lee

      #3
      Re: Switch..Case Statement Question.

      Andy said:[color=blue]
      >
      >Can someone tell me if the following Switch...Case construct is valid?
      >I'm wanting to check for multiple values in the Case statement without
      >explicitly listing each values.
      >So for example, will case 1-35: work? If this would work, will it
      >consider all numbers between 1 and 35 inclusive of 1 and 35? Please let
      >me know if the following will work below:
      >
      >switch(state ){
      >case 1-35:
      >case 37:
      > do something_1;
      > break;
      >case 36:
      > do something_2;
      > break;
      >case 38-50:
      > do something_3;
      > break;
      >}[/color]

      With less effort and much less wait time, you could have just tested
      your sample code yourself. Your first case will match any state that
      equals your index value of -34 (1-35).

      Comment

      • Andy

        #4
        Re: Switch..Case Statement Question.

        I apologize for not having tested myself and wasted your time. Do you
        or anyone else knows of a way to check for multiple values without
        stating each one like if I'd like to check all values from 1 through 35
        how would i do it?

        Any help will be appreciated.

        Comment

        • Michael Winter

          #5
          Re: Switch..Case Statement Question.

          Andy wrote:
          [color=blue]
          > Can someone tell me if the following Switch...Case construct is valid?[/color]

          No.
          [color=blue]
          > I'm wanting to check for multiple values in the Case statement without
          > explicitly listing each values.[/color]

          You cannot. The expression after the case keyword is compared for
          strict equality (===) with the expression in the switch statement. In
          other words,

          switch(2) {
          case '2': /* This statement list will not be evaluated. */
          }

          the statement list for case '2' will not be evaluated because the
          expressions do not evaluate to the same type (one is a number, the
          other is a string).

          You have little choice but to use an if statement.

          if(((1 <= state) && (35 >= state)) || (37 == state)) {
          /* Between 1 and 35 (inclusive) or is 37 */
          something_1();
          } else if(36 == state) {
          /* Is 36 */
          something_2();
          } else if((38 <= state) && (50 >= state)) {
          /* Between 38 and 50 (inclusive) */
          something_3();
          }

          [snip]

          Mike

          --
          Michael Winter
          Replace ".invalid" with ".uk" to reply by e-mail.

          Comment

          • Evertjan.

            #6
            Re: Switch..Case Statement Question.

            Andy wrote on 28 feb 2005 in comp.lang.javas cript:
            [color=blue]
            > I apologize for not having tested myself and wasted your time. Do you
            > or anyone else knows of a way to check for multiple values without
            > stating each one like if I'd like to check all values from 1 through 35
            > how would i do it?
            >[/color]

            Please do not use usenet as a form of email. you are replying to many
            here, so quote relevant part of the mail you answer on. Remember
            netiquette.

            Now "I" have to quote from your old posting:[color=blue]
            >switch(state ){
            >case 1-35:
            >case 37:
            > do something_1;
            > break;
            >case 36:
            > do something_2;
            > break;
            >case 38-50:
            > do something_3;
            > break;[/color]


            You could do:

            state = 20;
            switch(true){
            case state>=1&&state <=35:
            case state==37:
            do something_1;
            break;
            case state==36:
            do something_2;
            break;
            case state>=38&&stat e<=50:
            do something_3;
            break;
            case default:
            alert('default' )
            }

            This however in my view defies usefulness of switch().
            anyway if-else is much more robust:

            state = 20;
            if ((state>=1&&sta te<=35)||state= =37){
            do something_1
            else if (state==36)
            do something_2
            else if (state>=38&&sta te<=50)
            do something_3
            else
            alert('default' );

            [not tested]

            --
            Evertjan.
            The Netherlands.
            (Replace all crosses with dots in my emailaddress)

            Comment

            • Mark Preston

              #7
              Re: Switch..Case Statement Question.

              Andy wrote:
              [color=blue]
              > I apologize for not having tested myself and wasted your time. Do you
              > or anyone else knows of a way to check for multiple values without
              > stating each one like if I'd like to check all values from 1 through 35
              > how would i do it?
              >
              > Any help will be appreciated.
              >[/color]
              This is where the "greater than or equal to" and "less than or equal to"
              operators come in handy. For instance, ">=1 && <=35" is between one and
              thirty-five.

              Comment

              • bumbleguppy

                #8
                Re: Switch..Case Statement Question.

                You can also try

                switch(true){

                case(val>1 && val<35): do something;break ;
                case(val>=35 && val <53):do something;break ;

                Comment

                • Evertjan.

                  #9
                  Re: Switch..Case Statement Question.

                  bumbleguppy wrote on 05 mrt 2005 in comp.lang.javas cript:
                  [color=blue]
                  > You can also try
                  >
                  > switch(true){
                  >
                  > case(val>1 && val<35): do something;break ;
                  > case(val>=35 && val <53):do something;break ;
                  >[/color]

                  [please quote, usenet is not email]

                  is the above really more useful than:

                  if (val>1 && val<35) do_something;
                  if (val>=35 && val<53) do_something;

                  ?

                  --
                  Evertjan.
                  The Netherlands.
                  (Replace all crosses with dots in my emailaddress)

                  Comment

                  • rh

                    #10
                    Re: Switch..Case Statement Question.

                    Evertjan. wrote:[color=blue]
                    > bumbleguppy wrote on 05 mrt 2005 in comp.lang.javas cript:
                    >[color=green]
                    > > You can also try
                    > >
                    > > switch(true){
                    > >
                    > > case(val>1 && val<35): do something;break ;
                    > > case(val>=35 && val <53):do something;break ;
                    > >[/color]
                    >
                    > [please quote, usenet is not email]
                    >
                    > is the above really more useful than:
                    >
                    > if (val>1 && val<35) do_something;
                    > if (val>=35 && val<53) do_something;
                    >
                    > ?[/color]

                    I'd have to say "No", because the former is syntactically (block)
                    deficient. ;-)

                    However, the "switch/case" idiom can be more useful, or at least more
                    appropriate to use, in some circumstances because of the nature of the
                    structural encapsulation it provides for a specific filtering task.

                    The choice shouldn't have any effect on the result of the execution,
                    but it can have a marked effect on the readability and maintainability
                    of the code.

                    ../rh

                    Comment

                    • bumbleguppy

                      #11
                      Re: Switch..Case Statement Question.

                      Welcome to 2005 guy.

                      I don't know what archaic usenet application you are using to view this
                      thread, but I however am writing this reply in a text box in a webpage.
                      A Google webpage.

                      Why would I want to load an html page with the same quoted statemaents
                      ad naseum when, in my web browser, I can scoll up to see the relevent
                      subject material?

                      You remind me of the playground rules in tag. No electricity except in
                      freeze tag. No tagbacks.

                      You saw my post. You understood the meaning. Save your sophomoric
                      nerdish and arbitrary instructions to the next newbie that wants to
                      "fit in" with the geeks.

                      Comment

                      • Randy Webb

                        #12
                        Re: Switch..Case Statement Question.

                        bumbleguppy wrote:
                        [color=blue]
                        > Welcome to 2005 guy.
                        >
                        > I don't know what archaic usenet application you are using to view this
                        > thread, but I however am writing this reply in a text box in a webpage.
                        > A Google webpage.[/color]

                        A Google webpage that is not a Usenet application but an attempt at a
                        web-based application for interfacing to Usenet.
                        [color=blue]
                        > Why would I want to load an html page with the same quoted statemaents
                        > ad naseum when, in my web browser, I can scoll up to see the relevent
                        > subject material?[/color]

                        Are you actually stupid enough to believe that rubbish that you spout?

                        --
                        Randy
                        comp.lang.javas cript FAQ - http://jibbering.com/faq & newsgroup weekly

                        Comment

                        • Richard Cornford

                          #13
                          Re: Switch..Case Statement Question.

                          bumbleguppy wrote:[color=blue]
                          > Welcome to 2005 guy.
                          >
                          > I don't know what archaic usenet application you are
                          > using to view this thread, but I however am[/color]

                          Whatever _you_ may be doing is irrelevant. Usenet is a one-to-many
                          communication medium where the needs/wishes/interest of the many
                          outweigh all personal preferences. Saving yourself a little effort in
                          exchange for wasting the time of hundreds or thousands of others is
                          objectionable and likely to be considered rude.
                          [color=blue]
                          > writing this reply in a text box in a
                          > webpage. A Google webpage.[/color]

                          Recent changes in google's Usenet interface have rendered it on a par
                          with the very worst providers of web-based access to Usenet
                          (Forum4designer s.com) and the only reasonable advice now possible is to
                          abandon using google groups in favour of some superior web-based
                          provider or (preferably) access Usenet directly through an NTTP server
                          (as provided by any ISP worthy of the name) using real newsreader
                          software.
                          [color=blue]
                          > Why would I want to load an html page[/color]

                          comp.lang.javas cirpt is a plain text _only_ newsgroup.
                          [color=blue]
                          > with the same quoted statemaents
                          > ad naseum when,[/color]

                          The convention is to _only_ quote the material that is being respond to,
                          and trim the rest. It is done to provide a context for the response.
                          [color=blue]
                          > in my web browser, I can scoll up to see
                          > the relevent subject material?[/color]

                          What ever you can do says nothing bout what the many who read your posts
                          can do (or what they would prefer to do). You (alone) will never be the
                          arbiter of what it correct.
                          [color=blue]
                          > You remind me of the playground rules in tag. No
                          > electricity except in freeze tag. No tagbacks.[/color]

                          Games, and many other things, work best when everyone plays by the same
                          rules. The 'rules', the conventions for Usenet posting, have been around
                          for a long time (more than 20 years), evolved to suite the medium, and
                          can be found by reading the groups FAQ (the reading of which is a
                          pre-requisite for participation in any technical Usenet group that
                          provides a FAQ).
                          [color=blue]
                          > You saw my post. You understood the meaning. Save
                          > your sophomoric nerdish and arbitrary instructions to
                          > the next newbie that wants to "fit in" with the geeks.[/color]

                          Are you inviting that active hostility of regulars on this group?

                          Richard.


                          Comment

                          • Richard Cornford

                            #14
                            Re: Switch..Case Statement Question.

                            rh wrote:[color=blue]
                            > Evertjan. wrote:[color=green]
                            >> bumbleguppy wrote:[/color][/color]
                            <snip>[color=blue][color=green][color=darkred]
                            >>> switch(true){
                            >>> case(val>1 && val<35): do something;break ;
                            >>> case(val>=35 && val <53):do something;break ;[/color][/color][/color]
                            <snip>[color=blue][color=green]
                            >> is the above really more useful than:
                            >>
                            >> if (val>1 && val<35) do_something;
                            >> if (val>=35 && val<53) do_something;
                            >>
                            >> ?[/color]
                            >
                            > I'd have to say "No", because the former is syntactically
                            > (block) deficient. ;-)[/color]
                            <snip>

                            To be honest I find the former superior, but only because of one
                            omission in the latter; there should be an - else - in front of the
                            second - if - statement. The conditions are mutually exclusive and there
                            is no reason to evaluate the second after the first has evaluated to -
                            true.

                            Range testing in - if/else if/else - blocks seems more readable than
                            controlling a - switch - from a numeric or boolean literal and then
                            evaluating expressions in the - case - clauses (useful as that facility
                            may be in other contexts).

                            Richard.


                            Comment

                            • John W. Kennedy

                              #15
                              Re: Switch..Case Statement Question.

                              Evertjan. wrote:[color=blue]
                              > bumbleguppy wrote on 05 mrt 2005 in comp.lang.javas cript:
                              >
                              >[color=green]
                              >>You can also try
                              >>
                              >>switch(true ){
                              >>
                              >> case(val>1 && val<35): do something;break ;
                              >> case(val>=35 && val <53):do something;break ;
                              >>[/color]
                              >
                              >
                              > [please quote, usenet is not email]
                              >
                              > is the above really more useful than:
                              >
                              > if (val>1 && val<35) do_something;
                              > if (val>=35 && val<53) do_something;[/color]

                              Actually, yes. You're missing an 'else'.

                              ---
                              John W. Kennedy
                              "Those in the seat of power oft forget their failings and seek only the
                              obeisance of others! Thus is bad government born! Hold in your heart
                              that you and the people are one, human beings all, and good government
                              shall arise of its own accord! Such is the path of virtue!"
                              -- Kazuo Koike. "Lone Wolf and Cub: Thirteen Strings" (tr. Dana Lewis)

                              Comment

                              Working...