Precedence and Associativity of Operators

Neso Academy
14 Apr 201816:27

Summary

TLDRThis lecture delves into the critical concepts of operator precedence and associativity in programming. It explains how precedence determines the order of evaluation when multiple operators are present, using examples like multiplication vs. addition. The lecture then clarifies associativity, which comes into play when operators share the same precedence, and it decides the direction of evaluation (left-to-right or right-to-left). The instructor uses a table to illustrate various operators' precedence and associativity, emphasizing the importance of understanding these concepts for correct program behavior. The lecture concludes with a homework problem to test comprehension.

Takeaways

  • 🔢 **Operator Precedence**: Determines the order in which operators in an expression are evaluated, with higher precedence operators being evaluated first.
  • 🔄 **Multiplication vs. Addition**: Multiplication has higher precedence than addition, so it is evaluated first in expressions.
  • 🔁 **Associativity**: Comes into play when operators have the same precedence, deciding whether operations are performed left-to-right or right-to-left.
  • 🔄 **Division and Multiplication**: Both have the same precedence, so their order is determined by associativity, which is left-to-right for these operators.
  • 📚 **Precedence Table**: A reference that lists operators by their level of precedence, from highest (at the top) to lowest (at the bottom).
  • 👥 **Parentheses**: Have the highest precedence and are used to explicitly specify the order of operations in an expression.
  • 🔄 **Postfix vs. Prefix**: Postfix increment/decrement operators have higher precedence than prefix ones, and their associativity is left-to-right, unlike prefix which is right-to-left.
  • 🔄 **Associativity of Unary Operators**: Unary operators, like prefix and postfix, have right-to-left associativity, which is different from most other operators.
  • ❗ **Undefined Behavior**: The order in which functions are called when there's only one operator (like assignment) is not defined and can vary by compiler.
  • 📝 **Homework Problem**: A reminder that understanding precedence and associativity is crucial for predicting the behavior of complex expressions in programming.

Q & A

  • What is the precedence of operators and how does it affect expression evaluation?

    -Operator precedence determines the order in which operators in an expression are evaluated when multiple operators are present. An operator with higher precedence is evaluated before an operator with lower precedence.

  • Can you provide an example to illustrate the concept of operator precedence?

    -Yes, in the expression 5 + 3 * 2, the multiplication operator (*) has higher precedence than the addition operator (+), so 3 * 2 is evaluated first resulting in 6, and then 5 + 6 equals 11.

  • What is the associativity of operators?

    -Associativity of operators comes into play when the precedence of the operators is the same. It determines whether an expression involving operators of the same precedence is evaluated from left to right or right to left.

  • How does the associativity of division and multiplication affect the evaluation of an expression?

    -Division and multiplication have the same precedence, so the associativity determines the order of evaluation. If associativity is left to right, division is performed first followed by multiplication.

  • What is the significance of parentheses in an expression?

    -Parentheses have the highest precedence among all operators and are used to explicitly specify the order of operations in an expression, overriding the default precedence and associativity rules.

  • Why are member access operators mentioned in the script, and what is their role?

    -Member access operators are used to access members of structures and are mentioned to highlight their precedence, which is higher than that of the assignment operator.

  • What is the difference between postfix and prefix increment/decrement operators in terms of precedence and associativity?

    -Postfix increment/decrement operators have higher precedence than prefix increment/decrement operators. Also, the associativity of postfix operators is from left to right, whereas for prefix operators it is from right to left.

  • How does the precedence and associativity of operators affect the evaluation of a program?

    -The precedence and associativity of operators dictate the order in which expressions are evaluated, which can significantly affect the outcome of a program. Understanding these concepts is crucial for predicting and controlling program behavior.

  • What is the associativity of the assignment operator, and why is it important?

    -The associativity of the assignment operator is from right to left, which is important for understanding how compound assignments are evaluated in expressions.

  • What does it mean when the script says that the behavior of calling functions in an expression is undefined?

    -It means that if two functions are called in an expression separated by an operator with which they both associate, the order in which the functions are called is not specified by the language standard, and can vary between different compilers or even different runs with the same compiler.

  • How can understanding precedence and associativity help in writing clearer code?

    -Understanding precedence and associativity helps in writing clearer code by allowing developers to use parentheses effectively to avoid ambiguity and to ensure that expressions are evaluated in the intended order.

Outlines

00:00

🔢 Understanding Operator Precedence and Associativity

This paragraph introduces the concepts of operator precedence and associativity in programming. Operator precedence determines the order in which operators are evaluated in an expression when multiple operators are present. For instance, multiplication has higher precedence than addition, meaning it is evaluated first. Associativity comes into play when operators have the same precedence, dictating whether operations are performed from left to right or right to left. The paragraph uses examples to illustrate these concepts, emphasizing their importance in programming.

05:00

📚 Precedence and Associativity of Various Operators

This section delves into the precedence and associativity of different operators, starting with the highest precedence operators such as parentheses and moving down to lower precedence operators like the comma operator. It explains how parentheses are used to clarify that an operand is a function rather than a variable, due to their high precedence over the assignment operator. The paragraph also touches on member access operators, postfix increment/decrement operators, and their precedence relative to prefix counterparts. The discussion highlights the significance of understanding these concepts to determine the order of evaluation in expressions.

10:02

🔄 Associativity and Undefined Behavior in Expressions

The paragraph discusses the importance of associativity when operators have the same precedence and how it can affect the order of evaluation. It clarifies that associativity only applies when there are two or more operators of the same precedence. The text also addresses the concept of undefined behavior in programming, where the order in which functions are called in an expression is not specified by the language, leading to potentially different outcomes depending on the compiler. This underscores the need for programmers to be aware of how their code may be interpreted by different compilers.

15:02

📝 Homework Problem on Operator Precedence and Associativity

In the final paragraph, a homework problem is presented to the audience, challenging them to apply their understanding of operator precedence and associativity to predict the output of a given C program fragment. The paragraph summarizes the lecture's key points and invites viewers to engage with the material by sharing their answers in the comments section. The problem is designed to reinforce the concepts discussed throughout the lecture and to encourage active learning and discussion among viewers.

Mindmap

Keywords

💡Precedence

Precedence refers to the priority of operators in an expression, determining which operations are performed first when multiple operators are present. It is crucial for understanding how expressions are evaluated in programming languages. In the script, the example given is an expression with both a multiplication and an addition operator. The precedence rule states that multiplication, having a higher precedence, is evaluated before addition, leading to the correct result of 17.

💡Associativity

Associativity defines the order in which operations of the same precedence level are performed. It comes into play when the operators in an expression have the same level of precedence. The script explains that if operators like multiplication and division have the same precedence, associativity (left-to-right or right-to-left) determines the evaluation order. The example used is 10 divided by 2 times 5, which equals 25 if evaluated left-to-right, but equals 1 if evaluated right-to-left.

💡Expression

An expression in programming is a combination of values, variables, operators, and functions that computes to a single value. The script discusses how expressions are evaluated based on the precedence and associativity of the operators they contain. Understanding expressions is fundamental to writing and interpreting code that performs calculations.

💡Operator

An operator is a symbol that tells the compiler to perform specific mathematical or logical operations. The script covers various types of operators, such as arithmetic, bitwise, and assignment operators, and how their precedence and associativity affect the evaluation of expressions.

💡Parenthesis

Parenthesis are used to explicitly specify the order of operations within an expression by increasing the precedence of the operations inside them. The script mentions that parenthesis have the highest precedence and are used to force the evaluation of the enclosed expression before others, such as in function calls.

💡Member Access Operators

Member access operators are used to access members (like variables and functions) of structures or objects. The script notes that these operators have a higher precedence than the assignment operator, which is important when accessing members for assignment or function calls.

💡Postfix Increment/Decrement

Postfix increment/decrement operators are used to increase or decrease a variable's value by one, after the variable's value has been used in an expression. The script points out that postfix increment/decrement operators have a higher precedence than their prefix counterparts, which is important when these operators are used in complex expressions.

💡Logical NOT

The logical NOT operator is a unary operator that inverts the truth value of a boolean expression. The script includes this operator in the precedence table, indicating its high precedence level, which is important for understanding how boolean expressions are evaluated.

💡Conditional Operator

The conditional operator is a ternary operator that selects one of two values depending on the evaluation of a boolean expression. The script highlights that the conditional operator has right-to-left associativity, which is unique compared to most other operators.

💡Assignment Operator

The assignment operator is used to assign values to variables. The script explains that the assignment operator has lower precedence than many other operators, which is why expressions enclosed in parentheses are evaluated before assignments in statements like 'int var = fun();'

💡Comma Operator

The comma operator is used to separate multiple expressions where the entire expression evaluates to the value of the last sub-expression. The script mentions that the comma operator has left-to-right associativity and is often used in for-loops for multiple discrete operations.

Highlights

Precedence of operators determines the order of evaluation in an expression.

Operators with higher precedence are evaluated first.

Multiplication has higher precedence than addition.

Associativity comes into play when operators have the same precedence.

Associativity can be either left to right or right to left.

Division and multiplication have the same precedence, and their associativity is left to right.

Parenthesis has higher precedence than the assignment operator.

Member access operators are used to access members of structures.

Postfix increment/decrement operators have higher precedence than prefix increment/decrement operators.

Associativity of postfix operators is from left to right, while prefix is from right to left.

Unary operators have associativity from right to left.

The order of evaluation for functions in an expression is not defined and is compiler-dependent.

Associativity only works when there are more than one operators of the same precedence.

Operators with the same precedence will have the same associativity.

The output of a program can be compiler-dependent when the order of function calls is not defined.

Understanding operator precedence and associativity is crucial for predictable program behavior.

Transcripts

play00:00

In this lecture we are going to talk about

play00:02

precedence and associativity of operators.

play00:08

Precedence of operators come into picture

play00:11

when in an expression we need to decide

play00:15

which operator will be evaluated first.

play00:18

Suppose, you have multiple operators in an expression.

play00:21

Then which operators will be evaluated first

play00:24

is decided by the precedence.

play00:27

Operator with higher precedence will be evaluated first.

play00:31

And this is so obvious.

play00:33

Like for example, we have this expression

play00:36

in which we have a plus operator

play00:39

and we have a multiplication operator.

play00:41

Maybe this particular operator will be evaluated first

play00:45

and then it will produce the answer

play00:47

as 17 because five into three is 15

play00:50

and 15 plus two is 17.

play00:52

Or maybe it is possible that this

play00:54

particular operator will be evaluated first

play00:57

and it will result the value 25.

play01:00

Two plus three, which is equal to five.

play01:03

Five into five is equal to 25.

play01:05

Right? Now, which one is correct?

play01:09

Precedence of multiplication is greater than the addition.

play01:13

Please note down.

play01:14

Precedence of multiplication is greater than the addition.

play01:18

Therefore, this one is correct

play01:21

and this one is not correct.

play01:25

Because precedence of multiplication is greater,

play01:28

therefore, three into five will be evaluated first

play01:31

and then after that, addition would be performed.

play01:34

Therefore, the final output is 17 and not 25. Okay.

play01:40

Now, what is associativity of operators?

play01:44

Associativity of operators come into picture

play01:47

when precedence of operators are same.

play01:51

Please note down.

play01:52

When precedence of operators are same,

play01:55

then only associativity comes into picture.

play01:58

And we need to decide which operator will be evaluated first.

play02:02

Of course, this whole thing will be done for the purpose

play02:06

that which operator will be evaluated first.

play02:10

Let's consider one example.

play02:11

Here in this example, you will find out two operators

play02:15

divide and multiply in a same expression.

play02:18

The obvious question is,

play02:20

which one will be evaluated first.

play02:23

Associativity can be either left to right or right to left.

play02:28

These are the only two possibilities.

play02:31

If the associativity is left to right,

play02:33

then division will be performed first

play02:36

and then multiplication because we are going from left to right

play02:40

and division will be performed first

play02:43

and multiplication will be performed next.

play02:45

Let me tell you one important point that,

play02:47

division and multiplication both are having the same precedence.

play02:53

Therefore, the associativity came into picture.

play02:55

And here, we need to decide the order of evaluation

play02:59

with the help of associativity and not by precedence.

play03:03

If we are thinking about the associativity is going from left to right,

play03:08

then division will be performed first

play03:10

and then the multiplication .

play03:12

Therefore, the final output is 25.

play03:14

Because, 10 divided by two, is equal to five

play03:18

and five into five is equal to 25.

play03:23

If associativity is from right to left,

play03:26

then multiplication will be performed first and then division.

play03:29

Two into five is 10 and 10 divided by 10 equals to one.

play03:34

Right? Now, which one is correct?

play03:36

Let me tell you one thing.

play03:37

Division will be performed first and then multiplication.

play03:41

Because, these two operators are having the same precedence

play03:44

and their associativity is actually from left to right.

play03:49

And not from right to left.

play03:51

Therefore, this one is correct

play03:54

And this one is not correct. Okay.

play03:58

Now, let's consider the precedence and associativity table.

play04:02

In this particular table you will find out

play04:05

different kinds of operators mentioned.

play04:08

Apart from that, their associativities are also mentioned.

play04:11

The topmost set of operators are

play04:13

having the highest precedence among all the operators.

play04:17

And the bottom most is having the least precedence.

play04:20

First of all, we are going to talk about these operators.

play04:23

These are parenthesis, these are brackets.

play04:26

Then we have some member access operators.

play04:28

And then we have postfix increment and decrement operators.

play04:33

Let's talk about all these operators one by one.

play04:38

Parenthesis is used in function calls, right?

play04:42

Suppose, for example, we have a variable

play04:44

and to this variable we are trying to assign

play04:47

the value returned by this function.

play04:50

But, my question is

play04:52

how compiler would be able to know

play04:54

that this particular operand is a function

play04:58

and not a variable.

play05:00

It can be a variable as well.

play05:01

Why it is a function and not a variable?

play05:04

Assignment operator is also fighting for this operand

play05:08

that this particular operand belongs to me.

play05:12

And these parenthesis also are fighting for this particular operand

play05:17

that this particular operand belongs to me.

play05:19

Now, who wins the battle?

play05:21

Assignment operator is having less precedence as compared to parenthesis.

play05:27

Please note down.

play05:28

It is having less precedence.

play05:30

Therefore parenthesis belongs to fun

play05:33

and fun will be treated as a function.

play05:36

Therefore, we can say that after precedence, it looks like this.

play05:43

int var equal to fun()

play05:45

This fun is an operand.

play05:47

Of course, but it is a function and not a variable.

play05:52

Because parenthesis is having greater precedence than assignment operator.

play05:56

Therefore, this will be evaluated first

play05:59

and then, the assignment is being performed.

play06:02

I use parentheses to make this point clear

play06:05

that this particular operand is not a variable, it is a function.

play06:10

And because of the precedence,

play06:12

of this particular operator is greater than the assignment operator.

play06:16

Therefore, this operand belongs to this particular operator

play06:21

and not this operator.

play06:23

Suppose if assignment operator is having greater precedence than

play06:27

fun will belong to assignment operator

play06:30

and therefore it will be treated as a variable and not a function.

play06:35

Then in that case, this particular statement looks like this.

play06:39

Because, assignment operator is having greater precedence,

play06:43

therefore, it will be evaluated first

play06:45

and then after the parentheses will get evaluated.

play06:47

Therefore, this fun is treated as a variable and not a function.

play06:52

That is why precedence and associativity is important.

play06:55

Because, we need to know what would be the order of evaluation.

play07:00

Now, let's try to understand member access operators.

play07:05

These two are member access operators.

play07:10

They are used to access members of structures.

play07:13

Please note down this point.

play07:17

We will talk about structures later in this course.

play07:20

Therefore, there is no point in

play07:21

concentrating on these operators right now.

play07:26

Then we have postfix increment and decrement operators.

play07:30

Plus plus and minus minus.

play07:32

If you remember that immediately after these member access operators,

play07:35

we have postfix increment and decrement operators.

play07:39

One important point that we need to know

play07:42

related to these postfix increment and decrement operators is that,

play07:47

Precedence of postfix increment or decrement operator

play07:52

is greater than prefix increment or decrement operator.

play07:56

You will find out in the precedence table

play07:58

that prefix increment or decrement operator

play08:02

will immediately appear after

play08:04

postfix increment/decrement operator.

play08:07

Therefore, postfix increment or decrement operator is having

play08:12

greater precedence than prefix increment or decrement operator.

play08:17

Apart from that,

play08:19

Associativity of postfix is also different from prefix.

play08:23

This is also very important point.

play08:25

Associativity of postfix operators

play08:28

is from left to right and that of prefix operators is from right to left.

play08:34

Here, in this table, these are the postfix operators

play08:38

and these are the prefix operators.

play08:41

Because they appear first in the table,

play08:43

Therefore, they are having the greater precedence

play08:46

and this one is having the less precedence.

play08:49

Apart from that,

play08:50

associativity of postfix operators is from left to right.

play08:55

And associativity of prefix operators is from right to left.

play09:00

Okay.

play09:01

Apart for that, then we have logical not operator

play09:04

which we are well aware off.

play09:06

Then we have bitwise negation operator.

play09:09

Then of course, prefix increment or decrement operators.

play09:12

Then we have unary plus and unary minus operator.

play09:16

These operators might confuse you.

play09:18

They are unary operators and not binary operators.

play09:21

We can say that their signs, like plus 34, minus 36, like that.

play09:27

Start and ampersand as already told you,

play09:30

that we are going to cover these operators in pointers.

play09:33

This particular operator is used for typecasting,

play09:37

which we will discuss later on.

play09:39

And sizeof operator, we already discussed a lot.

play09:43

Apart from that,

play09:45

we have multiplicative operators list

play09:47

which consists of multiplication, division, and modulus.

play09:52

All of them are having precedence from left to right.

play09:55

Apart from that, we are having additive operators, like plus and minus.

play09:58

They are also having associativity from left to right.

play10:02

Then we have bitwise operators, relational operators,

play10:04

equality operators, bitwise AND, bitwise XOR,

play10:10

bitwise OR, bitwise AND, logical OR.

play10:14

Then we have a conditional operator.

play10:16

Associativity for conditional operator is right to left.

play10:20

If you find out, many conditional operators in an expression,

play10:24

then associativity for those conditional operators would be

play10:28

from right to left and not from left to right.

play10:31

Please remember this point.

play10:32

Apart from that, we have assignment operators

play10:35

and we already know one important point

play10:37

about assignment operators.

play10:39

That is their associativity is from right to left.

play10:43

Then final operator in this list of

play10:45

precedence and associativity of operators is comma operator.

play10:50

Associativity for comma operator is from left to right.

play10:54

Now the one most important point we need to know

play10:56

is that condition and assignment operators

play11:00

are having associativity from right to left and not from left to right.

play11:06

Apart from that,

play11:08

unary operators are also having associativity from right to left.

play11:13

And all the rest of the operators are having

play11:16

associativity from left to right.

play11:22

Let's discuss some important facts related to these operators.

play11:28

First of all,

play11:29

Associativity can only help if there are

play11:33

two or more operators of same precedence

play11:36

and not when there is just one operator.

play11:40

This is very important point.

play11:42

Associativity can only help you

play11:44

if there are at least two operators of same precedence

play11:48

and not when there is just one operator.

play11:50

In this particular example,

play11:52

we can easily see that we are calling

play11:54

two functions, fun one and fun two.

play11:57

And whatever the value they will return,

play11:59

finally, we add them using this addition operator.

play12:02

And the result of this addition will get stored

play12:05

inside this particular variable called a.

play12:08

And then we simply print this variable. Right?

play12:10

Here, in this function we are first trying to print Neso

play12:14

and then we return value one at this place.

play12:17

Here in this function, we are trying to print Academy

play12:20

and then we return one to this particular place.

play12:23

One plus one is equal to two

play12:26

and the output of this printf function is two.

play12:29

But my question is, which function will be called first?

play12:32

Is it fun1 or is it fun2?

play12:35

Most of us will give the answer as

play12:37

fun1 will be called first and then fun2.

play12:40

Because fun1 appears first in this expression.

play12:43

But this is not correct.

play12:45

So which function is called first?

play12:47

Is it fun1 or is it fun2?

play12:50

Now, let me give you the answer.

play12:52

It is not defined whether fun1 will be called first

play12:55

or whether fun2 will be called.

play12:57

Behavior is undefined and output is compiled a dependent.

play13:02

You cannot say that fun1 will be called first

play13:06

and then after fun2 will be called.

play13:08

Because fun1 appears first in this particular expression,

play13:12

therefore, it would be called first and then after fun2 will be called.

play13:15

It is not the case that always fun1 will be called first.

play13:20

It might be the possibility that some compilers

play13:23

will call fun2 first and then fun1.

play13:25

It is simply not defined.

play13:27

We cannot say anything about it.

play13:29

First of all, there is no precedence issue here

play13:32

because we just have one operator.

play13:33

Here of course, between this addition operator and assignment operator

play13:38

addition operator is having greater precedence.

play13:40

Therefore, it will be evaluated first.

play13:42

But my question is, which function will be called first?

play13:46

The output is compiled a dependent.

play13:49

Please note down, associativity will not

play13:52

come into picture as we just have one operator.

play13:55

And which function will be called first is undefined.

play14:00

Associativity will only work when we have

play14:02

more than one operators of same precedence.

play14:07

Now, what is the output of this particular program?

play14:10

Is it NesoAcademy2 or is it AcademyNeso2?

play14:15

If fun1 will be called first,

play14:17

then first Neso will be printed

play14:19

and then after one is returned.

play14:21

And then after fun2 will be called

play14:23

and Academy will get printed

play14:25

and it will return value one to this particular place.

play14:29

One plus one is equal to two

play14:31

which will get printed after printing NesoAcademy, right?

play14:35

So, output can be NesoAcademy2.

play14:38

But if fun2 to will be called first,

play14:40

then the output would be AcademyNeso2, right?

play14:44

So, what is the output?

play14:46

We simply don't know.

play14:51

Now, let's consider one more important fact.

play14:56

Operators with same precedence have same associativity as well.

play15:02

Here in this table, we can see that

play15:05

these all operators are having same precedence. Right?

play15:08

Although they are having the highest precedence

play15:11

among all the other operators in this list,

play15:14

but the operators, which are available

play15:17

in this particular block are all having the same precedence.

play15:21

And their associativity is also same, like left to right.

play15:25

Suppose we find out this operator as well as this operator

play15:29

within an expression, then of course

play15:31

the associativity will be the same, left to right.

play15:34

It doesn't make sense that this particular operator

play15:37

is having left to right associativity

play15:39

and this one is having right to left associativity.

play15:41

Similarly, all these operators are having same precedence.

play15:45

Therefore, their associativity would be also same,

play15:48

that is from right to left.

play15:50

And similarly, this concept is applicable to

play15:53

all the other operators as well.

play15:58

Now, let's consider one homework problem.

play16:00

What is the output of the following C program fragment?

play16:04

Is it true?

play16:06

Or is it false?

play16:07

You can always post your answers in the comments section below.

play16:10

Okay friends, this is it for now.

play16:13

Thank you for watching this lecture.

Rate This

5.0 / 5 (0 votes)

関連タグ
Precedence RulesAssociativity GuideProgramming ConceptsCode EvaluationMathematical OperatorsSyntax UnderstandingLogical OperatorsC ProgrammingExpression ParsingCompiler Behavior
英語で要約が必要ですか?