473,473 Members | 1,820 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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 40056
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
If replying to the group, please do not mail me too
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
If replying to the group, please do not mail me too
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
If replying to the group, please do not mail me too

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
If replying to the group, please do not mail me too
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
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

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
source to start with. If you change it to:

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
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" <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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

22
by: BekTek | last post by:
Hi.. I'm so sorry about that I've postes so many questions recently.. :) I'm still confused about the differences between malloc and operator new.. I know that when we work with class object and...
2
by: emma middlebrook | last post by:
Hi Having difficulty getting myself clear on how a type's operator== fits in with Object.Equals. Let's just consider reference types. The default operator== tests for object identity...
7
by: Alex Vinokur | last post by:
What is the difference between an operator and a function in C++? Alex Vinokur email: alex DOT vinokur AT gmail DOT com http://mathforum.org/library/view/10978.html...
4
by: Shrutisinha | last post by:
need a help in using between operator in tera data i want to ose as condition and other conditio too where H.BILLED_DT BETWEEN Max(H.BILLED_DT) AND add_months(Max(H.BILLED_DT),-1) ERROR IMPROPER...
4
by: DBC User | last post by:
This is a very simple question, I have a number and I want to check if the number is between 2 integers. I can use 'if' to resolve this situation. But I am curious, if there is any other verbs that...
3
by: veer | last post by:
hello expert i want to insert the records from one database to another on a particular condition i,e between two dates if i insert all the records it works fine but i need the records on...
2
by: orajit | last post by:
SELECT empno,sal,job FROM emp WHERE deptno IN (SELECT deptno FROM dept) AND to_char(joindate ,'ddmmyyyy') BETWEEN (START_DATE) AND (END_DATE); ...
3
by: C++Liliput | last post by:
Hi, I was looking at the implementation of operator new and operator new in gcc source code and found that the implementation is exactly the same. The only difference is that the size_t argument...
3
by: hiral amit patel | last post by:
when i am searching the names in range with following query select * from salesman where name between 'a%' and 'c%'; it does not display the names starting from c; it displays the name which...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.