views:

117

answers:

8

Hi, is the expression alright?

(A>=100 && B<100 || B<A)

I am not sure whether there should not be:

(A>=100 && (B<100 || B<A))

I need to say that when A>=100 AND (B<100 OR B < A).

+3  A: 

What you're talking about is operator precedence. The AND symbol has a higher precedence than OR, so in your first example the AND is calculated first. If you want the OR to be calculated first then, yes, you should include the parenthesis.

nukefusion
A: 
A>=100 && B<100 || B<A

is the implicit version of :

(A>=100 && B<100) || B<A

as the && operator has more priority than ||

mathieu
+1  A: 

Well, actually:

(A>=100 && (B<100 || B<A))

is the same as:

(A>=100 && B<A)

That's because, if B < 100, it's automatically less than A since A >= 100 and here's the code that proves it (in C but C# should be the same):

#include <stdio.h>
static void test (int a, int b) {
    printf ("a=%3d, b=%3d : ok=%d\n", a, b,
        (a>=100 && (b<100 || b<a)) == (a>=100 && b<a));
}
int arr[] = {1,2,3,99,100,101,199,200,201};

int main (void) {
    int i, j;
    for (i = 0; i < sizeof(arr)/sizeof(*arr); i++) {
        for (j = 0; j < sizeof(arr)/sizeof(*arr); j++) {
            test (arr[i], arr[j]);
        }
    }
    return 0;
}

It outputs:

a=  1, b=  1 : ok=1
a=  1, b=  2 : ok=1
a=  1, b=  3 : ok=1
a=  1, b= 99 : ok=1
a=  1, b=100 : ok=1
a=  1, b=101 : ok=1
a=  1, b=199 : ok=1
a=  1, b=200 : ok=1
a=  1, b=201 : ok=1
a=  2, b=  1 : ok=1
a=  2, b=  2 : ok=1
a=  2, b=  3 : ok=1
a=  2, b= 99 : ok=1
a=  2, b=100 : ok=1
a=  2, b=101 : ok=1
a=  2, b=199 : ok=1
a=  2, b=200 : ok=1
a=  2, b=201 : ok=1
a=  3, b=  1 : ok=1
a=  3, b=  2 : ok=1
a=  3, b=  3 : ok=1
a=  3, b= 99 : ok=1
a=  3, b=100 : ok=1
a=  3, b=101 : ok=1
a=  3, b=199 : ok=1
a=  3, b=200 : ok=1
a=  3, b=201 : ok=1
a= 99, b=  1 : ok=1
a= 99, b=  2 : ok=1
a= 99, b=  3 : ok=1
a= 99, b= 99 : ok=1
a= 99, b=100 : ok=1
a= 99, b=101 : ok=1
a= 99, b=199 : ok=1
a= 99, b=200 : ok=1
a= 99, b=201 : ok=1
a=100, b=  1 : ok=1
a=100, b=  2 : ok=1
a=100, b=  3 : ok=1
a=100, b= 99 : ok=1
a=100, b=100 : ok=1
a=100, b=101 : ok=1
a=100, b=199 : ok=1
a=100, b=200 : ok=1
a=100, b=201 : ok=1
a=101, b=  1 : ok=1
a=101, b=  2 : ok=1
a=101, b=  3 : ok=1
a=101, b= 99 : ok=1
a=101, b=100 : ok=1
a=101, b=101 : ok=1
a=101, b=199 : ok=1
a=101, b=200 : ok=1
a=101, b=201 : ok=1
a=199, b=  1 : ok=1
a=199, b=  2 : ok=1
a=199, b=  3 : ok=1
a=199, b= 99 : ok=1
a=199, b=100 : ok=1
a=199, b=101 : ok=1
a=199, b=199 : ok=1
a=199, b=200 : ok=1
a=199, b=201 : ok=1
a=200, b=  1 : ok=1
a=200, b=  2 : ok=1
a=200, b=  3 : ok=1
a=200, b= 99 : ok=1
a=200, b=100 : ok=1
a=200, b=101 : ok=1
a=200, b=199 : ok=1
a=200, b=200 : ok=1
a=200, b=201 : ok=1
a=201, b=  1 : ok=1
a=201, b=  2 : ok=1
a=201, b=  3 : ok=1
a=201, b= 99 : ok=1
a=201, b=100 : ok=1
a=201, b=101 : ok=1
a=201, b=199 : ok=1
a=201, b=200 : ok=1
a=201, b=201 : ok=1

But, if those were just general items rather than specific ones, && has a higher precedence than || in C#, so you should use:

(A>=100 && (B<100 || B<A))
paxdiablo
I don't think you have that quite right...?
Murph
paxdiablo
Kobi
Right, that should be better, and I've checked it this time :-)
paxdiablo
A: 

Basically no...

and (&&) has - or at least should have - precedence over or (||) - but regardless if there is any possibility of doubt write the expression in such a way that your are clear to the reader (and therefore to the compiler) about your intent.

To which end if you mean:

If A >= 100 and then if B < 100 or B < A you have to write

(A>=100 && (B<100 || B<A)) 

However in this specific example the B < 100 is redundant - which is, I think causing further confusion, all you need is

(A>=100 && B<A) 
Murph
A: 

(A >= 100 && B < 100 || B < A) is the same as ((A >= 100 && B<100) || B < A)

In this case, this will be true, in two cases:

1) A >= 100 and B < 100 (note, B < A automatically follows)

2) B < A < 100

(A >= 100 && (B < 100 || B < A))

This one will be true if

1) A is above 100 AND

2) B is below A

ie,. this is the same as (A >= 100 && B < A) (but B doesn't have to be below 100)

Zaki
A: 

Regarding operator precedence, see this SO post: C# conditional AND (&&) OR (||) precedence

Dev F
+1  A: 

A>=100 && B<100 || B<A doesn't make sense: it's equal to just B<A.

A>=100 && (B<100 || B<A) also doesn't make sense: it's equal to just A>=100 && B<A.

Michel de Ruiter
A: 

Do not you think this?

(A=<100 && B>100 || B<A)

I think there is typo in the OP questions

Bliznak