Arithmetic Expressions and Precedence.

 

Arithmetic Expressions and Precedence

Arithmetic Instruction:

A C arithmetic instruction consist of variable name on the left-hand side of "=" and variable name & constant on the right hand side of "=". The variable and constant appearing on the right side of = are connected by the arithmetic operators like "+", "-", "/", "*".
The variable and constants together are called operands. While executing arithmetic statements the operands on the right hand side are operated by the "arithmetic operators " and the result is then assigned using the assignment operator to the variable on the left-hand side.


Precedence of operators:

While executing an arithmetic statement, which has two or more operators, we may have some problem as to how to exactly does it get executed. The priority or precedence in which the operations in an arithmetic statement are performed is called the precedence of operators of hierarchy of operators.

Precedence of operators:
For example:

exmp = 2*3/4+2
exmp = 6/4+2     ( * precedence is greater than +, /)
exmp = 1.5+2     ( / precedence is greater than +)
exmp = 3.5         ( at last + )

Answer is exmp = 3.5.

Associativity of operators

When an operation contains two operators of the same priority the tie between them is settled using the associativity of operators. Associativity can be of two types left to right or right to left. Left to right associativity means that the left operand must be non-confusing. It must not be involved in the evaluation of any other subexpression. Similarly, in the case of Right to left associativity the right operand is non-confusing.

Arithmetic Expressions and Precedence.

Post a Comment

0 Comments