Computer Science, asked by sneha1612, 1 year ago

what is meant by precedence order of operators

Answers

Answered by JAMES1111
8
Operator Precedence and Associativity in C

Operator precedence determines which operator is performed first in an expression with more than one operators with different precedence. For example 10 + 20 * 30 is calculated as 10 + (20 * 30) and not as (10 + 20) * 30.

Associativity is used when two operators of same precedence appear in an expression. Associativity can be either Leftto Right or Right to Left. For example ‘*’ and ‘/’ have same precedence and their associativity is Left to Right, so the expression “100 / 10 * 10” is treated as “(100 / 10) * 10”.

Precedence and Associativity are two characteristics of operators that determine the evaluation order of subexpressions in absence of brackets.

1) Associativity is only used when there are two or more operators of same precedence.
The point to note is associativity doesn’t define the order in which operands of a single operator are evaluated. For example consider the following program, associativity of the + operator is left to right, but it doesn’t mean f1() is always called before f2(). The output of following program is in-fact compiler dependent.

// Associativity is not used in the below program. Output

// is compiler dependent.

int x = 0;

int f1() {

  x = 5;

  return x;

}

int f2() {

  x = 10;

  return x;

}

int main() {

  int p = f1() + f2();

  printf("%d ", x);

  return 0;

}

2) All operators with same precedence have same associativity
This is necessary, otherwise there won’t be any way for compiler to decide evaluation order of expressions which have two operators of same precedence and different associativity. For example + and – have same associativity.

3) Precedence and associativity of postfix ++ and prefix ++ are different
Precedence of postfix ++ is more than prefix ++, their associativity is also different. Associativity of postfix ++ is left to right and associativity of prefix ++.

4) Comma has the least precedence among all operators and should be used carefully For example consider the following program, the output is 1. See this and this for more details.

#include<stdio.h>

int main()

{

    int a;

    a = 1, 2, 3; // Evaluated as (a = 1), 2, 3

    printf("%d", a);

    return 0;

}

5) There is no chaining of comparison operators in C
In Python, expression like “c > b > a” is treated as “a > b and b > c”, but this type of chaining doesn’t happen in C. For example consider the following program. The output of following program is “FALSE”.

#include <stdio.h>

int main()

{

  int a = 10, b = 20, c = 30;

// (c > b > a) is treated as ((c > b) > a), associativity of '>'

  // is left to right. Therefore the value becomes ((30 > 20) > 10)

  // which becomes (1 > 20)

  if (c > b > a) 

   printf("TRUE");

  else

    printf("FALSE");

  return 0;

} FOR JAVA :::::::::Java has well-defined rules for specifying the order in which the operators in an expression are evaluated when the expression has several operators. For example, multiplication and division have a higher precedence than addition and subtraction. Precedence rules can be overridden by explicit parentheses.

Precedence order.

 When two operators share an operand the operator with the higher precedence goes first. For example, 1 + 2 * 3 is treated as 1 + (2 * 3), whereas 1 * 2 + 3 is treated as (1 * 2) + 3 since multiplication has a higher precedence than addition.

Associativity.

 When an expression has two operators with the same precedence, the expression is evaluated according to its associativity. For example x = y = z = 17is treated as x = (y = (z = 17)), leaving all three variables with the value 17, since the = operator has right-to-left associativity (and an assignment statement evaluates to the value on the right hand side). On the other hand, 72 / 2 / 3 is treated as (72 / 2) / 3 since the / operator has left-to-right associativity. Some operators are not associative: for example, the expressions (x <= y <= z) and x++-- are invalid.

Precedence and associativity of Java operators.

 The table below shows all Java operators from highest to lowest precedence, along with their associativity. Most programmers do not memorize them all, and even those that do still use parentheses for clarity.
Answered by Himanshu707
8
there is order of precedence for the different operators in programming. Just in the case of BODMAS, you first solve the equation in the brackets, the equations consisting 'of', then you perform divide, then multiply, then addition and then subtraction, ....in the same when you write any equation or formula in the code, compiler compiles it keeping in mind the order of precedence, so as to what to do first and then next and next.
Similar questions