3

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).

niton
  • 8,771
  • 21
  • 32
  • 52
Monty
  • 133
  • 1
  • 1
  • 4
  • It's not clear from your question what condition you are trying to test for - is it the second? (You say 'whether there should *not* be'). – Kieren Johnstone Aug 12 '10 at 07:33
  • 2
    You should give some examples of possible values of A and B, and your expected output. Also, note that `a>=100, b<100` -> `b – Kobi Aug 12 '10 at 07:34
  • I'll say that again, more clearly - the code is confusing, and the "plain text" explanation isn't better - they are both ambiguous - we cannot understand what you **mean**, so you cannot get a right answer. – Kobi Aug 12 '10 at 08:04
  • Changed English bit to make it explicit. I'm pretty certain that's what's wanted since the second code bit states it (and OP can change it to the other one if I'm wrong - I just wanted to remove ambiguity). – paxdiablo Aug 12 '10 at 08:09
  • @paxdiablo - How do you know your edit is correct? I'm afraid we don't have enough data to "remove ambiguity" - just three possibly wrong versions. – Kobi Aug 12 '10 at 08:10
  • I don't. I only know it's unambiguous. If it's wrong, the OP can fix it. It's a trick I do with user requirements. If they're vague, I send back an email stating my interpretation along with "Failing correction in the next 3 days, this is what we'll go with" :-) – paxdiablo Aug 12 '10 at 08:27

7 Answers7

3

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.

Matt B
  • 8,315
  • 2
  • 44
  • 65
1

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
  • 854,327
  • 234
  • 1,573
  • 1,953
1

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
  • 7,131
  • 5
  • 49
  • 74
0
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
  • 30,974
  • 4
  • 64
  • 90
0

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
  • 9,985
  • 2
  • 26
  • 41
0

(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
  • 1,101
  • 9
  • 7
0

Do not you think this?

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

I think there is typo in the OP questions

Bliznak
  • 531
  • 1
  • 4
  • 6