Lecture 12 - Expression and assignment

¡Supera tus tareas y exámenes ahora con Quizwiz!

Other notations for expression

Infix notation (infix operators + precedence rules + associativity rules) is only one way of representing arithmetic expressions. Other notations include - (Polish) prefix notation. - (Polish) postfix notation. - Cambridge prefix notation.

Operator Associativity Rules

Operator associativity defines the order in which "adjacent" operators of the same precedence levels are evaluated associativity rules are useful to resolve expressions like: 3/5*0 Normally left to right. Exponentiation operator is normally right to left

Boolean Expression

- C89 has no Boolean type - it uses int type with 0 for False and non-zero for True - One odd characteristic of C's expressions: a < b < c is a legal expression but the result is not what you might expect The left operator (a < b) is evaluated, producing 0 or 1, which is then treated a numeric value (rather than a boolean) and is then compared with the third operand (c)

Compound Assignment Operators

- Compound means combining assignment with arithmetic operators. e.g., +=, -+ *= /=

Operator precedence

1. "(" ")" 2. unary operators "++", "--" 3. Unary operators "+", "-" 4. Binary operators "*", "/" 5. Binary operators: "+", "-"

Operators

A unary operator has one operand. For example, - Negation operator "-": -b where b is the operand When the operators precede (or follow) their operands, we say the operator are prefix (infix). Unary operator could be prefix of postfix. e.g., increment operator "++", ++i and i++ A binary operator has two operands e.g. + - * / In most languages, binary operators are infix, which means they appear between their operands

Cambridge Prefix Notation

Cambridge notation introduces parentheses into prefix notation e.g. a + b - c * d would be written as (-(+ab) (*cd)) An advantage: It makes operators like "+" and "-" become n-nary e.g., a+b+c+d would be written as (+abcd) Lisp uses this notation

Issues of Notation

In absence of associativity and precedence rules, the infix notation is inherently ambiguous e.g., if a=3, b=4, and c=5, the value of the expression a + b * c would be 35 if evaluated from left to right, and 23 if right to left

Type Conversions in Assignment

- Different languages take different approaches to the type compatibility between the left- and right-hand sides. - For some languages it has to be done explicitly like Pascal and Java Coercion: - In other languages, e.g., in C,, the conversion is done automatically by the complier via coercion. - Coercion is an implicit type conversion that is initiated by the compiler. - The compiler checks the type compatibility of the assignment and changes the type of right side of the assignment

Assignment as an Expression

- In the C-based languages (e.g., Java, Perl and JavaScript), the assignment operator "=" is treated in the same way as other binary operators (e.g. "+"). - i.e., assignment returns a vale, but it has the side effect of changing its left operand (i.e., the effect of assignment) - For example, in C and Java, expression x = y + 1; returns a value (which equals the value assigned to the variable on left) and assigning the value to x is (just) a side-effect.

Operator Overloading

- Operator Overloading is the use of an operator for more than one purpose - E.g., in Java "+" is used for int and float additions but it is also the operator for String concatenation. - Overloading could cause problems/difficulties - E.g., ampersand "&" in C and C++ specifically both bitwise AND operation (binary) and "address of" (unary) operations - Compiler error detection might be affected (e.g., missing operand x in x&y ) - C++ and C#, allow user-defined overloaded operators when sensibly used, such operators can be an aid to readability (avoid using method calls, expression appear natural)

Postfix Notation

- Operators appear after their operands. - it is also evaluated from left to right but by combining the two operands with the operator after them: a+b-c*d becomes ab+cd*- or abcd*-+ - PostScript (printer control language) uses this notation.

Prefix Notation

- Operators appear before their operands. - It is evaluated from left to right by combing the two operands with the operator in front of them (using parenthesis to show the order of evaluation): -(+ab) * cd then -(+ab) (*cd) then, in the 2nd pass (-(+ab) (*cd)) - Prefix notation is inherently unambiguous

Relational Expressions

- Relational operator is a binary operator that compare the values of its operands and the result is a Boolean value True or False. - JavaScript and PHP have tow additional equality operators: === and !==. They are similar to == and !=, but prevent their operands being coerced: - "7"==7 would be evaluated as True, where string "7" is coerced to number 7, whereas - "7"===7 is False.

Potential Problems with Short Circuit Evaluation

- Short-circuit evaluation of Boolean expressions allows subtle errors to occur. E.g., (a>b || b++>3) - In this expression, b++>3 is evaluated (therefore b changes) only when a>b is false (i.e., a<=b). - If the programmer assumes that b++>3 is evaluated every time (i.e., treated it as standard evaluation), the program will fail. - In short-circuit evaluation, the order of the expression being typed by the programmer is important. It does not allow the compiler to reorder and prune expressions as it sees fit.

Assignment

- The general syntax <target_var> < assign_operator> <expression> - The assignment operator = Fortran, BASIC, the C-based languages := Pascal, ada - In many languages, <target_var> could be one of the following: - a variable name; - a field/attribute of a record/object (e.g. p.name)' - an indexed variable (e.g. a[i]); - a dereferenced pointer (e.g. *ptr).

Type Conversion in Arithmetic Expressions

- Two conversion rules: widening & narrowing - A widening conversion is one in which an object is converted to a type that allows any possible value of the original data e.g. int to float Widening conversion is safe and widely used - A narrowing conversion convert data type to a potentially smaller memory space, possibly losing accuracy in the process It is usually done explicitly as a request from the programmers who feel such conversion is necessary. The conversion is not safe (overflow may happen)

Expressions

An expression is a combination of values, variables, operators and function calls. Expressions perform operations on data and move data around. For example arithmetic expression: 4 * I + func(5) relational expression: a >= b Boolean expression: A&&B assignment: i = 4 Various expressions are instrument for implementing various algorithms. To correctly evaluate expressions, we need to know the rules of precedence and the rules of associativity of the operator.

Arithmetic Expressions

Arithmetic evaluation was one of the motivations for the development of the first programming languages. Arithmetic expressions consist of: - operators, - operands, - parentheses, and; - function calls.

Short Circuit Evaluation

Short circuit evaluation: a way of expression evaluation in which the result is determined without evaluating all the operands and/or operators. Example: (13*a) * (13/b - 1) - If a is zero, the whole expression evaluates to zero, therefore there is no need to evaluate (13/b -1) - But such case is not easy to detect, so short circuit evaluation like this is never taken Another example: (a>=0) && (b<10) - The value of the Boolean expression is independent of the second relational expression if a>=0 is false - Unlike the case of arithmetic expressions, this shortcut can be easily discovered and taken during execution. Many programming languages provide both standard and short-circuit versions of the boolean operators. E.g., C++, C#, Java, Matlab, etc provide both: - &, | (standard) and - &&, || (short-circuit version) Some do not and some allow compilers to choose

Semantics of assignment expressions

There's only a difference between compound assignments when the following happens: a[i++] *= 2; which is different from a[i++] = a[i++] * 2 because a is evaluated twice in the second one

Side effect of Expression

We say an expression has a side effect if, in addition to return a value (the main aim of an expression), it also modifies variables or causes something else to happen Side effects come about when an expression includes: - an assignment - increment/decrement operations, - function call, or - method invocation e.g. a function might modify a global or static variable, or modify one of its arguments. Java example: int doSomething (int[] a) { //Do some operations on parameter a ... return aValue; } in x = doSomething(aArray); where expression x=doSomething() has a side effect of changing the value of aArray.

Unary Assignment Operators

sum = ++count (pre-increment count, then assign to sum) sum = count++ (post-increment count, then assign to sum)


Conjuntos de estudio relacionados

K12 Chem 4.05 Chemical Thermodynamics

View Set

PrepU Chapter 32: Skin Integrity and Wound Care

View Set

Chapter 10 Reporting and Analyzing Liabilities

View Set

DSM-5 Study Guide Questions and Answers, DSM 5 ASWB EXAM STUDY GUIDE

View Set

EMTB CH 28 HW and Quiz Questions

View Set

ATI Medical-Surgical Practice Test: Immune and Infectious, ATI Immune Test, ATI Endocrine

View Set

Psyc 154 Lecture 20 Attitude to Behavior Relationship

View Set