# [Matlab] Order of precedence in computations

Tags:
1. Jul 30, 2014

### Maylis

Hello, here is a list of the order of precedence for computations when programming, at least in MATLAB

1. Parentheses (starting with innermost pair)
2. NOT (~) (left to right)
3. Arithmetic operations (left to right)
4. Relational operations (left to right)
5. AND (&)
6. OR (|)
7. Short-circuit AND (&&)
8. Short-circuit OR (||)

I was wondering if there is any rhyme or reason for this order, or is it just something that I need to have committed to memory?

2. Jul 30, 2014

### FactChecker

Double check those rules, correct them, and then memorize them. They are fairly standard in programming and in mathematics. Be sure to check the rules you posted. Line 3 is wrong. It would make 2 + 3 * 5 = (2 + 3) *5 = 25. That is wrong. Multiplication precedes addition so 2 + 3 * 5 = 2 + 15 = 17.

Also, use parentheses liberally to force the calculation that you want.

3. Jul 30, 2014

### Maylis

Great. I forgot an old trick for memorizing things, use mnemonics! PNARAOSS!!

4. Jul 31, 2014