In this video, we'll learn about operator precedence and associativity in c++.
C++ has well-defined operator precedence and associativity rules.
Here's the table showing the precedence and associativity
of some of c++ operators.
Note that this table is not complete.
It only shows a subset of c++ operators.
You can find complete operator precedence tables online
and in most c++ books.
Note that there's two columns. The first column shows the operator.
The operators on higher rows have higher precedence than operators on lower rows.
So as you can see the assignment operators are down at the bottom as you would expect
and the parenthesis operator is at the top.
Operators on the same row have the same precedence.
So for example, you can see that addition
and subtraction have the same precedence.
I think precedence is pretty easy to understand.
We learned operator precedence when we learned math and grammar school.
It's the same idea here except we have more operators to consider.
The second column is the associativity column.
This is the part that many times confuses students and even c++ programmers.
Notice that associativity is left to right or right to left.
Let's see what it means.
So what's associativity all about
suppose you have an expression with two adjacent operators and these operators are different,
like we see in the first example.
We can look for operators in the precedence chart,
and if one of the operators has higher precedence than the other,
then that's the operator that's applied first.
This is exactly what we've been doing with our math that we've learned.
But suppose the two operators are the same
or they're different but they have the same level of precedence.
Now precedence doesn't really help us.
In this case, we determine how the operators are applied by using their associativity.
In the second example, we have the same operator, op1, used adjacently.
If op1 associates left to right,
then we apply the operator to expression1 and expression2 first.
If op1 associates right to left,
then we apply the operator to expression2 and expression3 first.
Of course, you can always use parentheses to remove any doubt
and be sure that your result is what you expect.
Using parentheses is good practice with complex expressions.
Let's see a couple of simple examples.
In the first example, we have three operators:
the assignment operator, the addition operator and the multiplication operator.
The precedence chart tells us that the multiplication operator has the highest precedence,
followed by addition, followed by assignment.
So I've rewritten the same statement on line two,
showing the order of evaluation by including parentheses.
Note that the evaluation takes place in order of multiplication,
addition and finally, assignment.
In the second example, we again have three operators:
the assignment operator, the addition operator and the subtraction operator.
In this case, addition and subtraction are of higher precedence than assignment,
but they both have the same precedence.
So in this case, we need to use their associativity to determine the order of evaluation.
We see that plus and minus associate left to right.
I've rewritten this example using parentheses to show the order of evaluation.
Many beginning c++ programmers
keep an operator precedence chart handy when they first start out with c++.
After not too long, you get to know the operators and this becomes second nature
just like using math precedence did the more we used it in school.