between operator??

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

    between operator??

    I have a variable: int nbr= 5.
    I need to test if the nbr is <=10, between 11 and 15, between 16 and 20 or
    greater than 20.

    How can I do this?

    thanks.


  • Jon Skeet [C# MVP]

    #2
    Re: between operator??

    mgonzales3 <mgonzales3@dis cussions.micros oft.com> wrote:[color=blue]
    > I have a variable: int nbr= 5.
    > I need to test if the nbr is <=10, between 11 and 15, between 16 and 20 or
    > greater than 20.
    >
    > How can I do this?[/color]

    if (nbr <= 10)
    {
    // <= 10
    }
    else if (nbr < 15)
    {
    // 11-15
    }
    else if (nbr < 20)
    {
    // 16-20
    }
    else
    {
    // > 20
    }


    --
    Jon Skeet - <skeet@pobox.co m>
    http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
    If replying to the group, please do not mail me too

    Comment

    • Mark R. Dawson

      #3
      RE: between operator??

      Hi,
      you will have to write some conditional statements to check this logic,
      there is no inbuild between operator, like:

      int nbr = 5;
      if(nbr <= 10)
      {
      }
      else if(nbr >= 11 && nbr <=15)
      {
      }
      else if(nbr >= 16 && nbr <= 20)
      {
      }
      else if(nbr > 20)
      {
      }

      Now other way around it :-)

      Mark Dawson





      "mgonzales3 " wrote:
      [color=blue]
      > I have a variable: int nbr= 5.
      > I need to test if the nbr is <=10, between 11 and 15, between 16 and 20 or
      > greater than 20.
      >
      > How can I do this?
      >
      > thanks.
      >
      >[/color]

      Comment

      • Jon Skeet [C# MVP]

        #4
        RE: between operator??

        Mark R. Dawson <MarkRDawson@di scussions.micro soft.com> wrote:[color=blue]
        > you will have to write some conditional statements to check this logic,
        > there is no inbuild between operator, like:
        >
        > int nbr = 5;
        > if(nbr <= 10)
        > {
        > }
        > else if(nbr >= 11 && nbr <=15)
        > {
        > }
        > else if(nbr >= 16 && nbr <= 20)
        > {
        > }
        > else if(nbr > 20)
        > {
        > }
        >
        > Now other way around it :-)[/color]

        Well, the good news is that you don't need the >= tests here, due to
        the fact that you're already in "else" blocks. That removes the
        redundancy of specifying effectively the same number twice (one version
        always just being one higher than the other).

        --
        Jon Skeet - <skeet@pobox.co m>
        http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
        If replying to the group, please do not mail me too

        Comment

        • Mark R. Dawson

          #5
          RE: between operator??

          true - I put them in as looking at it is a no brainer, without it I had to
          think slightly when looking at it :-)

          "Jon Skeet [C# MVP]" wrote:
          [color=blue]
          > Mark R. Dawson <MarkRDawson@di scussions.micro soft.com> wrote:[color=green]
          > > you will have to write some conditional statements to check this logic,
          > > there is no inbuild between operator, like:
          > >
          > > int nbr = 5;
          > > if(nbr <= 10)
          > > {
          > > }
          > > else if(nbr >= 11 && nbr <=15)
          > > {
          > > }
          > > else if(nbr >= 16 && nbr <= 20)
          > > {
          > > }
          > > else if(nbr > 20)
          > > {
          > > }
          > >
          > > Now other way around it :-)[/color]
          >
          > Well, the good news is that you don't need the >= tests here, due to
          > the fact that you're already in "else" blocks. That removes the
          > redundancy of specifying effectively the same number twice (one version
          > always just being one higher than the other).
          >
          > --
          > Jon Skeet - <skeet@pobox.co m>
          > http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
          > If replying to the group, please do not mail me too
          >[/color]

          Comment

          • raviaw@gmail.com

            #6
            Re: between operator??

            Isn't better to use the >= test, to avoid mistakes and make it more
            "error prone" for futures changes that could be made?
            You now how people are..

            Comment

            • Jon Skeet [C# MVP]

              #7
              Re: between operator??

              <raviaw@gmail.c om> wrote:[color=blue]
              > Isn't better to use the >= test, to avoid mistakes and make it more
              > "error prone" for futures changes that could be made?
              > You now how people are..[/color]

              That depends - if you're *always* going to have adjacent ranges (i.e.
              every number is covered) then I believe it's better *not* to have the
              extra test. With my version, if you need to move one of the boundaries,
              you only have to change one number. With Mark's version you have to
              change two, making *that* more error prone.

              --
              Jon Skeet - <skeet@pobox.co m>
              http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
              If replying to the group, please do not mail me too

              Comment

              • Randy A. Ynchausti

                #8
                Re: between operator??

                mgonzales3,
                [color=blue]
                >I have a variable: int nbr= 5.
                > I need to test if the nbr is <=10, between 11 and 15, between 16 and 20 or
                > greater than 20.
                >
                > How can I do this?[/color]

                If the numbers are geometric, you can use a mathematical approach like this:

                class Program
                {
                static void Main(string[] args)
                {
                for (int i = 0; i < 30; i++)
                {
                int factor = i / 5;
                switch (factor)
                {
                case 0 :
                System.Console. WriteLine(i.ToS tring() + " < 5 [" +
                factor.ToString () + "]");
                break;
                case 1 :
                System.Console. WriteLine("5 < " + i.ToString() + " < 10 [" +
                factor.ToString () + "]");
                break;
                case 2:
                System.Console. WriteLine("10 < " + i.ToString() + " < 15 ["
                + factor.ToString () + "]");
                break;
                case 3:
                System.Console. WriteLine("15 < " + i.ToString() + " < 20 ["
                + factor.ToString () + "]");
                break;
                default:
                System.Console. WriteLine("20 < " + i.ToString() + " [" +
                factor.ToString () + "]");
                break;
                }
                }
                System.Console. WriteLine("Done ..");
                }
                }

                Regards,

                Randy


                Comment

                • Scott C

                  #9
                  Re: between operator??

                  Jon Skeet [C# MVP] wrote:[color=blue]
                  > <raviaw@gmail.c om> wrote:
                  >[color=green]
                  >>Isn't better to use the >= test, to avoid mistakes and make it more
                  >>"error prone" for futures changes that could be made?
                  >>You now how people are..[/color]
                  >
                  >
                  > That depends - if you're *always* going to have adjacent ranges (i.e.
                  > every number is covered) then I believe it's better *not* to have the
                  > extra test. With my version, if you need to move one of the boundaries,
                  > you only have to change one number. With Mark's version you have to
                  > change two, making *that* more error prone.
                  >[/color]

                  Come to think of it, I kind of like the multiple checking (except it
                  looks awful). Imagine just a simple drag/drop rearranging an
                  if/else/elseif statement from this:

                  if( i<10 ) {
                  // i<10
                  }
                  else if( i<20 ) {
                  // i<20
                  }
                  else if( i<30 ) {
                  // i<30
                  }
                  else {
                  // assume i>=30
                  }

                  with an "innocent" drag/drop to:


                  if( i<10 ) {
                  // i<10
                  }
                  else if( i<30 ) {
                  // i<30
                  }
                  else if( i<20 ) { // now this will *never* get called
                  // i<20
                  }
                  else {
                  // assume i>=30
                  }


                  .... Although, on second thought, if someone is going to rearrange things
                  like this, maybe we should just let them fall into the trap...

                  Scott

                  Comment

                  • Jon Skeet [C# MVP]

                    #10
                    Re: between operator??

                    Scott C wrote:[color=blue][color=green]
                    > > That depends - if you're *always* going to have adjacent ranges (i.e.
                    > > every number is covered) then I believe it's better *not* to have the
                    > > extra test. With my version, if you need to move one of the boundaries,
                    > > you only have to change one number. With Mark's version you have to
                    > > change two, making *that* more error prone.[/color]
                    >
                    > Come to think of it, I kind of like the multiple checking (except it
                    > looks awful). Imagine just a simple drag/drop rearranging an
                    > if/else/elseif statement from this:[/color]

                    <snip>

                    Maybe it's because I don't use drag and drop in code, but your scenario
                    seems less likely to me than someone changing:

                    if (i < 10)
                    {
                    }
                    else if (i >= 11 && i <= 15)
                    {
                    }
                    else if (i >= 15 && i < 20)
                    {
                    }

                    to (wanting to change a boundary from 15 to 13, say)

                    if (i <= 10)
                    {
                    }
                    else if (i >= 11 && i <= 13)
                    {
                    }
                    else if (i >= 16 && i < 20)
                    {
                    }

                    Of course, that's not a problem if you don't use magic numbers in your
                    source to start with. If you change it to:

                    if (i < FirstBoundary)
                    {
                    }
                    else if (i >= FirstBoundary && i < SecondBoundary)
                    {
                    }
                    else if (i >= SecondBoundary && i < ThirdBoundary)
                    {
                    }

                    You can then change boundaries with no problem.

                    The only difficulty there comes from *inserting* a boundary - you've
                    still got two bits of code to change (but you don't have to get the
                    ordering right).

                    Jon

                    Comment

                    • Scott C

                      #11
                      Re: between operator??



                      Jon Skeet [C# MVP] wrote:[color=blue]
                      > Maybe it's because I don't use drag and drop in code, but your scenario
                      > seems less likely to me than someone changing:
                      >
                      > to (wanting to change a boundary from 15 to 13, say)
                      >
                      > if (i <= 10)
                      > {
                      > }
                      > else if (i >= 11 && i <= 13)
                      > {
                      > }
                      > else if (i >= 16 && i < 20)
                      > {
                      > }
                      >
                      > Of course, that's not a problem if you don't use magic numbers in your
                      > source to start with. If you change it to:
                      >
                      > if (i < FirstBoundary)
                      > {
                      > }
                      > else if (i >= FirstBoundary && i < SecondBoundary)
                      > {
                      > }
                      > else if (i >= SecondBoundary && i < ThirdBoundary)
                      > {
                      > }
                      >[/color]

                      Hmm. Good point. I didn't think about the "insert" option. Come to
                      think of it, I rarely need this level of switching. Seems to me 99% of
                      the time I only care about one of two things:

                      // equals or not
                      if(a==b) {
                      // something
                      }else {
                      // something else
                      }

                      --or--
                      // range checking
                      if(lo < a && a < hi) {
                      // something
                      } else {
                      // not in range something
                      }


                      scott

                      Comment

                      • raviaw@gmail.com

                        #12
                        Re: between operator??

                        That's true

                        Comment

                        • Mike

                          #13
                          Re: between operator??


                          Assuming this isn't in the middle of some inner weather-prediction loop, you
                          could do:

                          opRet = DoRange( valueInQuestion ,
                          0, 10, new delegate(int x) { doThis(x); return someMore(); },
                          11, 15, new delegate { return doThat(); },
                          40, 99, new delegate { doSomethingElse (); return 1; }
                          200, int.MaxValue, new delegate(int x) { return wowThatsBig(x*x ); }
                          );

                          // UNTESTED

                          delegate void MyOp( int value);
                          public int DoRange( params object[] stuff )
                          {
                          int value = (int) params[0];
                          for ( int i = 1; i < stuff.Length; i+=3)
                          {
                          int begin = (int) stuff[ 0+i];
                          int end = (int) stuff[ 1+i];
                          MyOp fn = (MyOp) stuff[ 2+i];

                          if ( value >= begin && value <= end)
                          // note: if a value can be in more than one range, we can do
                          them all instead of returning
                          return fn( value);
                          }
                          }

                          If you don't need to return values, it's even simpler.
                          Of course, you could generisize all the types, and use interfaces or virtual
                          fn's instead of the anonymous delegates, support items in a list rather than
                          just the range, binary search if you can flag them in order, etc.

                          Then again, it's not too much prettier than the IF...
                          Anyone up for porting CL LOOP to c#? :)
                          using CL;
                          CLVar v; Hashtable h = ...;
                          Loop( FOR, v, BEING, THE, HASH-VALUE, OF, h, COUNT, new delegate(object
                          o) { (string o).StartsWith(" foo"); }, COLLECT ....


                          m

                          "mgonzales3 " <mgonzales3@dis cussions.micros oft.com> wrote in message
                          news:94CAF26F-F45F-40C8-8C64-3A7FF74B470D@mi crosoft.com...[color=blue]
                          >I have a variable: int nbr= 5.
                          > I need to test if the nbr is <=10, between 11 and 15, between 16 and 20 or
                          > greater than 20.
                          >
                          > How can I do this?
                          >
                          > thanks.
                          >
                          >[/color]


                          Comment

                          Working...