operator precedence and associativity

7
Operator Precedenc e and Associati vity • Two binary operator symbols must never be placed side by side. For example, 5*%6 is invalid because two operators, * and %, are placed next to each other.

Upload: nicole-ynne-estabillo

Post on 13-Jan-2017

52 views

Category:

Education


1 download

TRANSCRIPT

Page 1: Operator Precedence and Associativity

Operator Precedence

and Associativity

• Two binary operator symbols must never be placed side by side. For example, 5*%6 is invalid because two operators, * and %, are placed next to each other.

Page 2: Operator Precedence and Associativity

Operator Precedence

and Associativity

• Parentheses can be used to form groupings, and all expressions enclosed in parentheses are evaluated first. In this way, you can use parentheses to alter the evaluation to any order. For example, in the expression (6+4)/(2+3), the 6+4 and 2+3 are evaluated first to yield 10/5. The 10/5 is then evaluated to yield 2.

Page 3: Operator Precedence and Associativity

Operator Precedence

and Associativity

• Parentheses can be enclosed by other parentheses. For example, the expression (2*(3+7))/5 is valid and evaluates to 4. When parentheses are included within parentheses, expressions in the innermost parentheses are always evaluated first. The evaluation continues from innermost to outermost parentheses until all expressions in parentheses have been evaluated. The number of closing parentheses, ), must always equal the number of opening parentheses, (, so that no unpaired sets exist.

Page 4: Operator Precedence and Associativity

Operator Precedence

and Associativity

• Parentheses can’t be used to indicate multiplication; instead, the multiplication operator, *, must be used. For example, the expression (3+4)(5+1) is invalid. The correct expression is (3+4)*(5+1).

Page 5: Operator Precedence and Associativity

Operator Precedence

and Associativity

Parentheses should specify logical groupings of operands and indicate the intended order of arithmetic operations clearly to the compiler and programmers. Although expressions in parentheses are always evaluated first, expressions containing multiple operators, whether enclosed in parentheses or not, are evaluated by the priority, or precedence, of the operators.

Page 6: Operator Precedence and Associativity

There are three levels of precedence: 1. P1—All negations are done first. 2. P2—Multiplication, division, and modulus operations are computed next. Expressions containing more than one multiplication, division, or modulus operator are evaluated from left to right as each operator is encountered. For example, in the expression 35/7%3*4, all operations have the same priority, so the operations are performed from left to right as each operator is encountered. The division is done first, yielding the expression 5%3*4. The modulus operation, 5%3, is performed next, yielding a result of 2. Finally, the expression 2*4 is computed to yield 8.

Page 7: Operator Precedence and Associativity

3. P3—Addition and subtraction are computed last. Expressions containing more than one addition or subtraction are evaluated from left to right as each operator is encountered.