Please enable JavaScript.
Coggle requires JavaScript to display documents.
Expression - Coggle Diagram
Expression
What are Expressions
fundamental means of specifying computations in a programming language
is formed from operators, operands(literals, variables), parentheses, function calls and resolves to some value.
In function calls operands are known as arguments or parameters.
Classification of operators
Who defines
Pre-defined operators
User-defined operators
Number of operands
Unary(one operands)
Unary + is termed as identity operator
In many languages unary + has no effect on its operands
Negation(Unary Minus) : Changes the sign of a numerical value
Logical NOT : Flips the truth value of a boolean expression
Increment(++), decrement(--) operators are also unary operators
Binary(two operands)
Many operands(e.g : functions)
Ternary(Three operands)
n C,C++, python and Java ( ? : ) is used as a ternary operator
Expression_1 ? Expression_2 : Expression_3
If Expression_1 is evaluated to true the value of the entire expression is that of Expression_2, else it is that of Expression_3
Example: minVal = (a<b) ? a: b
Expression_1 must be a Boolean expression
How they operate on data values
Simple data object operators
Composite data object operators
Bitwise operators
Examples
Boolean expression
Functional call expression
Arithmetic Expression
Linearization of Expressions
Use mathematical expressions within programs they must be linearized
Postfix notation
Operation symbol follows the list of operands
No need parentheses or rules of operator precedence
Reverse polish notation
Infix
Problems
When more than one notation is ambiguous unless parentheses are used
Suitable only to binary operations
Use set of implicit rules
Operator symbol is written between the two operands
Prefix notation
A variant of polish notation – used in LISP
Parentheses surround an operator and its operands
Expression is written as the operation symbol followed by the operands in order from left to right
No parentheses are needed to specify the order of evaluation
Evaluation of prefix expressions
If the next item in P is an operator push it on top of the stack. Keep track of the number of operands (say n) needed by the operator
If the next item in P is an operand, push it on top of stack
If the top n entries in the stack are operands needed for the last operator, replace the top n + 1 entries by the result obtained by applying the last operator on the n operands above it on the stack
Design issues
Relational and Boolean Expressions
Relational Expressions
Evaluate to some Boolean representation
Use relational operators and operands of various types
Operator symbols used vary somewhat among languages
JS and PHP have two additional relational operator
==
!=
Boolean Expressions
Operands are Boolean and the result is Boolean
C has no Boolean type
Assignment Expression
The purpose of an assignment is to change the value of a variable
In many language the symbol = is used to denote assignment operator not for compare two values
In imperative programming languages assignments play a dominant role
purpose
change the value of a variable
Side effects
can lead to difficult to understand code
useful for multiple assignment
more complicated assignments
unary assignment operators
compound assignment operators
conditional targets
multiple targets
inherited from ALGOL 68
C, C++, and Java treat = as an arithmetic binary operator
Disadvantage
Another kind of expression side effect
Operator overloading
Using the same operator for more than one purpose
Types of overloading
Same operator is used for the same action but with different data type
Same operator is used for similar actions
Same operator for completely different actions
User defined operator overloading
potential problems
Users can define nonsense operations
Readability may suffer
Type Mixing in Expression
Narrowing conversion
converts an object to a type that cannot include all of the values of the original type
Widening conversion
converted to a type that can include at least approximations to all of the values of the original type
Can lose precision
Magnitude is retained
Type conventions: Mixed mode
A mixed-mode expression is one that has operands of different types
A coercion is an implicit type conversion
Disadvantage of coercions
They decrease in the type error detection ability of the compiler
In most languages, all numeric types are coerced in expressions, using widening conversions
In Ada, there are virtually no coercions in expressions
Explicit type conversion also known as casts or type casts can be used
C : (int)angle
Ada : Float(Sum)
Order of evaluation of operators
Side effects occur when
A function changes one ofits parameters
A function changes a non local variable
Operand evaluation order
Variables
Fetch yhe value from memory
Constants
Iterals
Sometimes fetch from memory
Function References
The case of most interest
Parenthesized Expression
How to solve problem
Write the language definition to
demand that operand evaluation
order be fixed
Disadvantages
Limits some compiler optimizations
Write the language definition to disallowing functions with side effects
Disadvantages
Inflexibility of one way parameters and lack of non local references
No non local references in functions
No two way parameters in functions
Order of evaluation of operators
meaning of expressions
Govered by
precedence rules
The order in which "adjacent" operators of different types are evaluated
some languages are designed with no precedence
associativity rules
Ψ is right-associative
expression a Ψ b Ψ c must be evaluated from right
to left
Ψ is non-associative
expressions of the form a Ψ b Ψ c
Most common one is Left-to-Right
Ψ is left-associative
expression a Ψ b Ψ c must be evaluated from left to
right
Short circuit expressions
It is important to know if your language supports it
C, C++, and Java: use short-circuit evaluation for the
usual Boolean operators (&& and ||)
Determine the result of the expression without
evaluating all of the operands and/or operators
Side effects
Change the value of variables in an expression after
the execution of that expression