1/40
Looks like no tags are added yet.
Name | Mastery | Learn | Test | Matching | Spaced | Call with Kai |
|---|
No analytics yet
Send a link to your students to track their progress
Expressions
the fundamental means of specifying computations in a programming language
4 components of arithmetic expressions
Operators -- Operands -- Parentheses -- Function Calls
3 types of operators by operand count
Unary (1 operand) -- Binary (2 operands) -- Ternary (3 operands)
Ternary operator
has three operands -- used in C-based languages as a conditional expression -- syntax: expression_1 ? expression_2 : expression_3
2 actions of arithmetic expression implementation
fetching operands from memory -- executing arithmetic operations on those operands
6 design issues for arithmetic expressions
operator precedence rules -- operator associativity rules -- order of operand evaluation -- restrictions on side effects -- user-defined operator overloading -- mode mixing in expressions
Operator Precedence
defines the order in which operators of different precedence levels are evaluated
Unary Addition (+)
called the identity operator -- no associated operation -- no effect on its operand
Unary Minus (-) in Java/C#
causes implicit conversion of short and byte operands to int type
Operator Associativity
defines the order in which adjacent operators with the same precedence level are evaluated -- can be left or right associative
Typical associativity
left to right -- except ** which is right to left
APL associativity
all operators have equal precedence -- all associate right to left
Overriding precedence and associativity
done with parentheses
Functional side effect
occurs when a function changes one of its parameters or a global variable
2 solutions to functional side effects
write language definition to disallow side effects -- write language definition to demand fixed operand evaluation order
Java operand evaluation order
guarantees left-to-right order -- eliminates side effect ambiguity
Operator Overloading
the use of an operator for more than one purpose
& in C/C++ -- overloading problem
unary: address-of operator -- binary: bitwise AND -- using the same symbol for two unrelated operations harms readability
in Java -- overloading
addition for numbers -- string catenation for strings
Languages allowing user-defined operator overloading
C++ -- C# -- F#
2 potential problems of user-defined operator overloading
users can define nonsense operations -- readability may suffer
Narrowing Conversion
converts an object to a type that cannot include all values of the original type -- example: double to float
Widening Conversion
converts an object to a type that can include at least approximations to all values of the original type -- example: int to float
Coercion in Expressions
implicit type conversion in a mixed-mode expression -- disadvantage: decreases type error detection ability of the compiler
Mixed-mode expression
an expression that has operands of different types
Languages with no coercions in expressions
ML -- F#
Mixed-mode assignment -- Fortran/C/C++
any numeric value can be assigned to any numeric scalar variable
Mixed-mode assignment -- Java/C#
only widening assignment coercions are done
Mixed-mode assignment -- Ada
no assignment coercion
Casts
explicit type conversions in the C-based languages
Relational Operator
an operator that compares the values of its two operands -- result is Boolean
Boolean in C (before C99)
no Boolean type -- uses int -- 0 is false -- nonzero is true
Short-Circuit Evaluation
result is determined without evaluating all operands and/or operators
Equal sign
Assignment operator symbols in ALGOL, Pascal, Ada
Compound Assignment Operators
shorthand where destination variable also appears as first operand on the right side
Unary Assignment Operators (++ and --)
combine increment/decrement with assignment -- can be prefix or postfix
Prefix ++ behavior
increments first then uses the value
Postfix ++ behavior
uses the value first then increments
Assignment as an Expression
treats assignment operator like a binary operator -- has side effect of changing its left operand -- leads to difficult-to-read expressions -- common source of errors in C
Multiple Assignments
languages that support multiple-target multiple-source assignments: Perl -- Ruby -- Lua
Assignment in Functional Languages
identifiers are only names of values -- no mutable assignment