10

I am creating a "Basic Search" bar that users can type in terms. I am unsure of the order of operations for boolean logic.

If someone types terms(With no quotes):

A and B or C

What is the correct way to treat this?

(A and B) or (C)

OR

(A) and (B or C)

Max87
  • 109
  • 1
  • 1
  • 3
  • 2
    I think this belongs in ux.stackexchange.com, but FWIW in standard Boolean logic the precedence rules make NOT highest, then AND, then OR. – dodgethesteamroller Jun 17 '13 at 16:24
  • @dodgethesteamroller Why? There's no mention of Unix,etc. in the question..? – RBarryYoung Jun 17 '13 at 16:27
  • @RBarryYoung The `ux` in ux.stackexchange.com is for "user experience," not Unix. My point is that there are implicit issues here about how to present Boolean search options--is the application such that the end users are expected to understand Boolean logic (e.g. in a programming context), or will they possibly be barely computer-literate (e.g. in a library card catalog search), or somewhere in between? If the OP is "unsure of the order of operations" then how confident is he that his users are not equally unsure? – dodgethesteamroller Jun 17 '13 at 16:38
  • @dodgethesteamroller Heh. My bad, sorry. – RBarryYoung Jun 17 '13 at 16:47
  • Lawyers are the target users. Most have no idea, some have training in searching technique...which is why I want to be sure of the order. – Max87 Jun 17 '13 at 17:22
  • @Max87: When lawyers are involved, assume nothing. ;) Seriously, use the established precedence rules, but put them right there on the screen. Assuming non-programmers understand Boolean logic is a major UI fail. – dodgethesteamroller Jun 17 '13 at 21:26
  • I just tried ((A) && (B) || (C)) in JS the result was same as in ((A && B) || (C)) then I tried it with ((A) || (B) && (C)) and this resulted as if it was ((A) || (B && C)). So I presume that there is an order similar to multiplication then adding with AND preceding OR – Orestes Kyriakos Poulakis Apr 15 '17 at 13:38

3 Answers3

14

Wikipedia to the rescue, this should help:

http://en.wikipedia.org/wiki/Order_of_operations#Programming_languages

From the looks of things, it would appear that AND takes precedence over OR in most languages.

Quetzalcoatl
  • 3,037
  • 2
  • 18
  • 27
12

Based on Quetzalcoatl's response, the correct answer for the OP question is:

(A and B) or C

That's the equivalent for "A and B or C"

Although Quetzalcoatl's link (http://en.wikipedia.org/wiki/Order_of_operations#Programming_languages) speaks about programming languages (as this site does), a more common precedence is specified for general logic in wikipedia:

https://en.wikipedia.org/wiki/Logical_connective#Order_of_precedence

Mathter
  • 747
  • 7
  • 14
-4

(A) and (B or C)

and means intersection or "like" union

Like in math

"AND" is like a multiplier and "OR" like a sum

in a "truth table"

OR
A | B | result
true | true | true
true | false | true
true | false | true
false | false | false

AND
A | B | result
true | true | true
true | false | false
true | false | false
false | false | false

Nuno Dias
  • 27
  • 1