Skip to main content

Section 12.3 The Order of Precedence

Figure 12.3.1. MATLAB operators have to wait for their turn.
There is actually one more important way that we can use relational operators, we can use them in combination with mathematical operators. For example, we COULD perform an operation like this.

Example 12.3.2. Order of Operations?

Here is an interactive code box that functions identically to the MATLAB command window.
The problem is that we have not discussed the order of precedence (link is to MATLAB’s description). This is very similar to the mathematical concept of the order of operations. The only difference is that the order of operations does not include the relational or logical operators (we will talk about them later). Just like the order of operations, the order of precedence lets us know what mathematical, relational, and logical operation steps will happen in what order. MATLAB’s order of precedence is presented in figure 12.3.3 below.
You can think about it like the operators are waiting in line to get into a club. The bouncer is going to let everyone in but they don’t all get to come in first come first serve. There is a hierarchy, and that hierarchy is listed in table 12.3.3 below.
Figure 12.3.3. The MATLAB Order of Precedence
You should note that this is slightly different than what MATLAB presents as the order of precedence, I truncated the official list down to what we will be using in this book. Let’s take another look at that MATLAB statement we started this section with:
pie + salmon^2 > tomato * salmon + 2
Now we can see how to apply the order of precedence and evaluate this statement! The best way to solve these types of statements is to reduce according to the order of precedence and then continue. For example:
  1. There are no parenthesis so we can skip that
  2. We need to evaluate the power. Since we have \(salmon^2\) we can evaluate that first. salmon = 7*8 implies that salmon^2 = \(56^2= 3136\)
  3. Now our statement has reduced to: pie + 3136 > tomato * salmon + 2 Next on the order of precedence is .*. We have one multiplication operation, tomato * salmon = \(23*56=1288\text{.}\)
  4. Now our statement has reduced to: pie + 3136 > 1288 + 2 We now perform the math operations. Recall pie = pi*2 which is \(\approx 6.2832\text{.}\) So… pie + 3136 = \(6.2832+3136=314.2832\) and 1288 + 2 = \(1230\)
  5. We are almost done. At this point we have: 3142.2832 > 1230 Lastly, we have a relational operator, >. We are now ready to evaluate it!

Checkpoint 12.3.4. Relational Operation Result.

Continuing with the previous example, what is the result of the relational operation?
  • False (0)
  • Incorrect. Re-evaluate the relational expression from the previous example.
  • True (1)
  • Correct. The relational operation evaluates to true, which MATLAB represents as 1.
Hopefully, you nailed Question 12.3.4 above. Let’s try one more little brain workout before we move on and discuss the Logical Operators.

Checkpoint 12.3.5. Order of Operations and Relational Expressions.

You have attempted of activities on this page.