Principles of Programming Language
–Operator precedence rules? –Operator associativity rules? –Order of operand evaluation? –Operand evaluation side effects? –Operator overloading? –Type mixing in expressions?
Arithmetic Expressions: Operators A unary operator has one operand A binary operator has two operands A ternary operator has three operands Arithmetic Expressions: Operator Precedence Rules The operator precedence rules for expression evaluation define the order in which “adjacent” operators of different precedence levels are evaluated Typical precedence levels
– parentheses – unary operators – ** (if the language supports it) – *, / – +, -
Arithmetic Expressions: Operator Associativity Rule which adjacent operators with the same precedence level are evaluated
•The operator associativity rules for expression evaluation define the order in
•Typical associativity rules –Left to right, except **, which is right to left –Sometimes unary operators associate right to left (e.g., in FORTRAN) •APL is different; all operators have equal precedence and all operators associate right to left
G. NARAYANAMMA INSTITUTE OF TECHNOLOGY & SCIENCE (For Women) DEPARTMENT OF IT
Principles of Programming Language
•Precedence and associativity rules can be overriden with parentheses
Arithmetic Expressions: Conditional Expressions Conditional Expressions
–C-based languages (e.g., C, C++) –An example: –Evaluates as if written like if (count == 0) average = 0 else average = sum /count
average = (count == 0)? 0 : sum / count
Arithmetic Expressions: Operand Evaluation Order Operand evaluation order
•Variables: fetch the value from memory •Constants: sometimes a fetch from memory; sometimes the constant is in the machine language instruction
•Parenthesized expressions: evaluate all operands and operators first •The most interesting case is when an operand is a