CSADPRG_f2f_expressions_assignment_statements
original file
this is unedited
Arithmetic Expressions
- primary goals of the first high-level lang was the automatic evaluation of arithmetic expressions
- notations: prefix, postfix, infix
- operators can be unary (single operant), binary, ternary
Operators
- symbols that denote logical operations, or any type of operations
- equals is assignment or equality
- plus%+, minus-, asterisk*, slash/, modulo%, equals=, less than<, more than>, parenthesis(), !, exponent^
- these are binary operators
Types of operators
- all arithmetic operators are binary operators
- arithmetic operations are evaluated using PEMDAS
- MD has the same level or precedence
Unary | Binary | Ternary |
---|---|---|
require one operand for operation | first operand on the left of the operator and second operator on the right | requires three operands. in js: one operator follows this -> conditional operator |
providing two or more would have a syntax error | x = x+10 | used to summarize or shorten code using conditionals |
increment x++ ++x | 3*x | int a = 10, b = 20, max; |
decrement +-- | 10+20 | |
typeof "hello" delete array[2]+"200" | 80<20 | |
const number = 20 |
Design Issues
- next meeting
Operator Precedence
- defines order in which adjacent operators of different precedence levels are evaluated
Operator Associativity
- defines order in which adjacent operators with the same precedence level are evaluated
- left-to-right evaluation for the same precedence
Symbol Table
- compiler uses this in place of PEMDAS
- association shows you which one goes first on the same precedence, use only when there's the same precedence level
Symbol | Precedence | Association |
---|---|---|
+- | 1 | R->L |
*/ | 2 | L->R |
^ | 3 | R->L |
% | 4 | L->R |
1+2*3-4
parenthesis method -> makes it single operand
for this class: keep writing the full thing but use parenthesis method
- (3-4) = -1
- (1+2) = 3
- (
-1*3
)
A+B*C-D^E/F%G-H+1
A+B*C-D^E/F%G-(H+1)
first level precedence R->LA+B*C-D^E/F%(G-(H+1))
A+B*(C-D)^E/F%(G-(H+1))
(A+B)*(C-D)^E/F%(G-(H+1))
(A+B)*(C-D)^E/F%(G-(H+1))
((A+B)*(C-D))^E/F%(G-(H+1))
second level precedence L->R((A+B)*(C-D))^(E/F)%(G-(H+1))
((A+B)*((C-D))^(E/F))%(G-(H+1))
third level precedence but there's only one ^ so it doesn't matter and we dont need associativity(((A+B)*((C-D))^(E/F)))%((G-(H+1)))
fourth level precedence
Typical Precedence Levels
Ruby | C Based |
---|---|
** | postfix++, -- |
unary+, - | prefix++, --, unary+, - |
- | |
- |
left
- associative
-
- /
- %
- binary+
- binary-
right - associative
- check ppt
Operand Evaluation Order
- variables -> evaluated by fetching values from memory
- constants
- parenthesized expressions -> evaluate all operands and operators first
Functional Side Effect
- when calling a function changes one of its parameters or a global var
- to solve:
- disallow functional side effects
- no two-way parameters in functions, no nonlocal references in functions
disadvantages - in c: need to place the values in a struct
- global variables are necessary when efficiency is importnat
- or you can write a language definition to state the order of evaluating operands and demand that implementors guarantee that order
disadvantage
- limits compiler optimizations that involve reordering operant evaluations
Overloading/Operator overloading
- multiple use of an operator
- acceptabliity inhibits readability or reliability
- string concat + vs normal +
- dangers: using the same symbol is detrimental to readability and mistakes can go undetected by the compiler (ex. c ampersand)
- c++ and c# allows overloading
Narrowing Conversion
- converts a value to a type that cannot store even approx of all the values of the original type
- happens in float and double -> when double becomes a float
- long -> integer
- float -> int
Widening Conversion
- converts a value to a type that can include at least approximations of all the values of the original type
- safer than narrowing -> becomes more accurate
Mixed mode expressions
- expressions can have operands of different types
- coercion is an implicit type conversion initiated by the compiler or the runtime system -> changing the datatype of a
int a;
float b, c, d;
d = b * a; //type casting or type coercion
// there's more floats in the line + being assigned to a float value d