Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AND OR order of operations

Tags:

What is the equivalent of this statement?

if(cond1 AND cond2 AND cond3 OR cond4 AND cond5 AND cond6) 

Is it

if((cond1 AND cond2 AND cond3) OR (cond4 AND cond5 AND cond6)) 

Or

if(cond1 AND cond2 AND (cond3 OR cond4) AND cond5 AND cond6) 

Or

if(((cond1 AND cond2 AND cond3) OR cond4) AND cond5 AND cond6) 

ect...

This has been something that I've always been scared approaching, to which I just surround the conditions in parenthesis ( ). It would be great if my mind could be settled.

like image 202
Isaac Avatar asked May 29 '13 04:05

Isaac


People also ask

Which comes first in order of operations?

First, we solve any operations inside of parentheses or brackets. Second, we solve any exponents. Third, we solve all multiplication and division from left to right. Fourth, we solve all addition and subtraction from left to right.

Do you add first or multiply first?

Order of operations tells you to perform multiplication and division first, working from left to right, before doing addition and subtraction. Continue to perform multiplication and division from left to right. Next, add and subtract from left to right.

What are the 6 order of operations?

Applying the Order of Operations (PEMDAS) The order of operations says that operations must be done in the following order: parentheses, exponents, multiplication, division, addition, and subtraction.


1 Answers

A good way to remember this is to think of it mathematically.

  • AND as * (multiply)
  • OR as + (addition)
  • TRUE as 1
  • FALSE as 0

So thinking of it as simple math you get this:

  • 0 * 0 = 0
  • 1 * 0 = 0
  • 1 * 1 = 1
  • 0 + 0 = 0
  • 1 + 0 = 1
  • 1 + 1 = 1

Only thing that may be a tiny bit confusing is 1 + 1 = 1, but a bit can't go above 1. But it makes sense if you think of 1 as any non-zero number.

So with this in mind you can then apply this logic:

if(cond1 AND cond2 AND cond3 OR cond4 AND cond5 AND cond6)

Becomes:

if(cond1 * cond2 * cond3 + cond4 * cond5 * cond6)

See: https://en.wikipedia.org/wiki/Order_of_operations

like image 167
Deantwo Avatar answered Oct 20 '22 09:10

Deantwo