472,146 Members | 1,369 Online

# 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.
Feb 8 '06 #1
12 38064
mgonzales3 <mg********@discussions.microsoft.com> wrote:
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?

if (nbr <= 10)
{
// <= 10
}
else if (nbr < 15)
{
// 11-15
}
else if (nbr < 20)
{
// 16-20
}
else
{
// > 20
}
--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
Feb 8 '06 #2
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
http://www.markdawson.org

"mgonzales3" wrote:
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.

Feb 8 '06 #3
Mark R. Dawson <Ma*********@discussions.microsoft.com> wrote:
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 :-)

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 - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
Feb 8 '06 #4
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:
Mark R. Dawson <Ma*********@discussions.microsoft.com> wrote:
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 :-)

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 - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet

Feb 9 '06 #5
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..

Feb 9 '06 #6
<ra****@gmail.com> wrote:
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..

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 - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
Feb 9 '06 #7
mgonzales3,
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?

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.ToString() + " < 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
Feb 9 '06 #8
Jon Skeet [C# MVP] wrote:
<ra****@gmail.com> wrote:
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..

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.

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
Feb 9 '06 #9
Scott C wrote:
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.

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:

<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

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

Feb 9 '06 #10

Jon Skeet [C# MVP] wrote:
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

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

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
Feb 9 '06 #11
That's true

Feb 9 '06 #12

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
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" <mg********@discussions.microsoft.com> wrote in message
news:94**********************************@microsof t.com...
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.

Feb 9 '06 #13

### This discussion thread is closed

Replies have been disabled for this discussion.