Switch..Case Statement Question.

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

    #16
    Re: Switch..Case Statement Question.

    John W. Kennedy wrote on 06 mrt 2005 in comp.lang.javas cript:
    [color=blue]
    > Evertjan. wrote:[color=green]
    >> bumbleguppy wrote on 05 mrt 2005 in comp.lang.javas cript:
    >>
    >>[color=darkred]
    >>>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'.[/color]

    That being my [inconsequental in this example] mistake
    does not per se make it less useful then the switch(true)


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

    Comment

    • Lee

      #17
      Re: Switch..Case Statement Question.

      bumbleguppy said:[color=blue]
      >
      >Welcome to 2005 guy.
      >[/color]
      [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]

      Don't be so quick to rule out the possibility that you lack the
      experience to understand why the request is reasonable. That's
      a really good way to make yourself appear to be spectacularly
      ignorant.

      Insulting people for making a request whose value you fail to
      understand makes you look that much worse.

      Comment

      • rh

        #18
        Re: Switch..Case Statement Question.

        Richard Cornford wrote:

        <..>
        [color=blue]
        > 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[/color]
        facility[color=blue]
        > may be in other contexts).[/color]

        So it would seem, although I think that's in large part due to lack of
        some Javascript switch/case facility that's seen in other languages.

        In the greater scheme of things, I tend to view the distinction between
        switch/case and if/else as the former being more for conditioning
        (i.e., setup) and the latter more for program flow control. Of course,
        if/else is always favoured and appropriate when the set of cases to be
        distinguished is small.

        Not a perfect substitute, but perhaps helpful in the context of numeric
        range filtering, would be something like:

        <script type="text/javascript">

        Number.prototyp e.inRng = function( str ) {
        var testSet = str.split( "," );
        for ( var k = 0; k < testSet.length; k++ ) {
        var rng = testSet[ k ]
        .replace( /(\d|y)\s*-\s*([+-]?(\d|I))/g, "$1#$2" )
        .split( "#" );
        if ( typeof rng[ 1 ] == "undefined" ) rng[ 1 ] = rng[ 0 ];
        if ( this >= rng[ 0 ] && this <= rng[ 1 ] ) return true;
        }
        return false;
        }

        var z = 33;

        switch ( true ) {

        case z.inRng( '1-35, 37' ) :
        //..
        break;

        case z == 36 :
        //..
        break;

        case z.inRng( '38-50' ) :
        //..
        break;

        default :
        //..
        }
        </script>

        ../rh

        Comment

        • Evertjan.

          #19
          Re: Switch..Case Statement Question.

          rh wrote on 06 mrt 2005 in comp.lang.javas cript:[color=blue]
          > Number.prototyp e.inRng = function( str ) {[/color]

          Try this:

          <script type="text/javascript">
          var myCase = 'myCase';
          var mydo = 'mydo';
          var myDefault = 'myDefault';
          var res,a;

          function mySwitch(){
          var base = mySwitch.argume nts[0]
          for (var i=1;i<mySwitch. arguments.lengt h;i++){
          a = mySwitch.argume nts[i]
          if(i==0)var val=a
          else if (a==myCase)res= false
          else if ((a==mydo&&res) ||a==myDefault) {
          eval(mySwitch.a rguments[++i])
          return }
          else if (!res){
          if (/\d+\-\d+/.test(a))
          res = (base>=1*a.repl ace(/-\d+/,''))&&
          (base<=1*a.repl ace(/\d+-/,''))
          else
          res = (base==a)
          }
          }
          }


          mySwitch(

          6,

          myCase,2,3,4,
          mydo,"alert('Tw oThreeFour')",
          myCase,"5-7",8,
          mydo,"alert('Fi veToEight')",
          myCase,9,100,
          mydo,"alert('Ni neOrHundred')",
          myDefault,
          "alert('NoneOfT hem')"
          )

          </script>



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

          Comment

          • rh

            #20
            Re: Switch..Case Statement Question.

            Evertjan. wrote:[color=blue]
            > rh wrote on 06 mrt 2005 in comp.lang.javas cript:[color=green]
            > > Number.prototyp e.inRng = function( str ) {[/color]
            >
            > Try this:
            >[/color]

            <..>
            [color=blue]
            >
            > mySwitch(
            >
            > 6,
            >
            > myCase,2,3,4,
            > mydo,"alert('Tw oThreeFour')",
            > myCase,"5-7",8,
            > mydo,"alert('Fi veToEight')",
            > myCase,9,100,
            > mydo,"alert('Ni neOrHundred')",
            > myDefault,
            > "alert('NoneOfT hem')"
            > )
            >[/color]

            Well, you'll have to be given points for a adopting an inventive and
            novel approach, but I don't think I want to go there.

            Note that as soon as you call the "mySwitch" function, you've lost the
            current execution context. That means that if you want to do something
            that's actually useful in a "mydo", such as test or assign to a local
            variable from the original context, you're snookered.

            ../rh

            Comment

            • John W. Kennedy

              #21
              Re: Switch..Case Statement Question.

              Evertjan. wrote:[color=blue]
              > John W. Kennedy wrote on 06 mrt 2005 in comp.lang.javas cript:
              >
              >[color=green]
              >>Evertjan. wrote:
              >>[color=darkred]
              >>>bumblegupp y wrote on 05 mrt 2005 in comp.lang.javas cript:
              >>>
              >>>
              >>>
              >>>>You can also try
              >>>>
              >>>>switch(true ){
              >>>>
              >>>> case(val>1 && val<35): do something;break ;
              >>>> case(val>=35 && val <53):do something;break ;
              >>>>
              >>>
              >>>
              >>>[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'.[/color]
              >
              >
              > That being my [inconsequental in this example] mistake
              > does not per se make it less useful then the switch(true)[/color]

              I once saw, in the real world, in a production program, 100 (yes,
              literally 100) mutually exclusive cases IF'ed without a single 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

              • Dr John Stockton

                #22
                Re: Switch..Case Statement Question.

                JRS: In article <d0feth02ikr@dr n.newsguy.com>, dated Sun, 6 Mar 2005
                09:33:37, seen in news:comp.lang. javascript, Lee
                <REM0VElbspamtr ap@cox.net> posted :[color=blue]
                >bumbleguppy said:[color=green]
                >>
                >>Welcome to 2005 guy.
                >>[/color]
                >[color=green]
                >>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]
                >
                >Don't be so quick to rule out the possibility that you lack the
                >experience to understand why the request is reasonable. That's
                >a really good way to make yourself appear to be spectacularly
                >ignorant.
                >
                >Insulting people for making a request whose value you fail to
                >understand makes you look that much worse.[/color]

                With the consequence, which probably does need to be spelt out, that the
                regular experts will ignore the questions of such posters, confining
                themselves to correcting the errors that they make in their responses,
                if any, to others.

                Whatever suits the convenience of the expert regulars increases the
                chances of getting good answers.

                --
                © John Stockton, Surrey, UK. ?@merlyn.demon. co.uk Turnpike v4.00 MIME ©
                Web <URL:http://www.uwasa.fi/~ts/http/tsfaq.html> -> Timo Salmi: Usenet Q&A.
                Web <URL:http://www.merlyn.demo n.co.uk/news-use.htm> : about usage of News.
                No Encoding. Quotes before replies. Snip well. Write clearly. Don't Mail News.

                Comment

                Working...