Download presentation
Presentation is loading. Please wait.
Published byBathsheba Montgomery Modified over 6 years ago
1
Chapter 7: Expressions and Assignment Statements
Lectures # 14
2
Chapter 7 Topics Introduction Arithmetic Expressions
Overloaded Operators Type Conversions Relational and Boolean Expressions Short-Circuit Evaluation Assignment Statements Mixed-Mode Assignment Chapter 7: Expressions and Assignment Statements 2
3
Introduction Expressions are the fundamental means of specifying computations in a programming language. To understand expression evaluation, need to be familiar with the orders of operator and operand evaluation. Chapter 7: Expressions and Assignment Statements 3
4
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. Operators can be: A unary operator has one operand. A binary operator has two operands. A ternary operator has three operands. Chapter 7: Expressions and Assignment Statements 4
5
Unary Arithmetic Operators in C/C++ and Java
Unary Plus ( +A ): Examples: +num, +num/5, num1 * +num2 Has no effect on its operand in C/C++. In Java, it causes an implicit conversion of a char, short, or byte operand to int type; otherwise, it has no effect on its operand. Unary Minus ( -A ): Examples: -num, -num/5, num1 * -num2 Chapter 7: Expressions and Assignment Statements 5
6
Unary Arithmetic Operators in C/C++ and Java (cont.)
The pre-increment is specified as follows: (++a) It says to add 1 to the current value of the variable and then to use the new value (if necessary). The post-increment is specified as follows: (a++) It says to use the current value of the variable (if necessary) and then add 1 to it. The pre-decrement is specified as follows: (--a) It says to subtract 1 from the current value of the variable and then to use the new value (if necessary). The post-decrement is specified as follows: (a--) It says to use the current value of the variable (if necessary) and then subtract 1 from it. Chapter 7: Expressions and Assignment Statements 6
7
Binary Arithmetic Operators in C/C++ and Java
Operation Operator Example Operand Result Addition + A + B Both integers One operand is not integer Integer Double precision floating-point Subtraction - A – B Multiplication * A * B Division / A / B Modulus (Remainder) % A % B Chapter 7: Expressions and Assignment Statements 7
8
Ternary Operator in C/C++ and Java
A ternary operator “ ?: ” has three operands. average = (count==0)? 0 : sum/count; Evaluates as if written like: if (count == 0) average = 0; else average = sum/count; Chapter 7: Expressions and Assignment Statements 8
9
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. ** (the exponential operator, if the language supports it). *, /, % +, - APL has a single level of precedence. Chapter 7: Expressions and Assignment Statements 9
10
Arithmetic Expressions: Operator Associativity Rule
The operator associativity rules for expression evaluation define the order in which adjacent operators with the same precedence level are evaluated. Typical associativity rules: Left to right, except **, which is right to left. APL is different; all operators have equal precedence and all operators associate right to left. Precedence and associativity rules can be overridden with parentheses. Chapter 7: Expressions and Assignment Statements 10
11
Arithmetic Expressions: Conditional Expressions
C-based languages (e.g., C, C++). An example: average = (count == 0)? 0 : sum/count; Evaluates as if written like: if (count == 0) average = 0; else average = sum/count; Chapter 7: Expressions and Assignment Statements 11
12
Operand Evaluation Order: Functional Side Effects
A side effect of a function occurs when a function changes either: one of its parameters or a global variable. Assume fun(x) changes parameter x: y = fun(x) + x; y = x + fun(x); Chapter 7: Expressions and Assignment Statements 12
13
Functional Side Effect: Example (1 of 2)
int a = 5; int fun1() { a = 17; return 3; } void main() { a = a + fun1(); cout << a << endl; } // 8 is printed Chapter 7: Expressions and Assignment Statements 13
14
Functional Side Effect: Example (2 of 2)
int a = 5; int fun1() { a = 17; return 3; } void main() { a = fun1() + a; cout << a << endl; } // 20 is printed Chapter 7: Expressions and Assignment Statements 14
15
Overloaded Operators Operator Overloading is accomplished by defining functions that have the same name as the operator being overloaded. Some are common (e.g., +, -, *, … for int and float). C++, FORTRAN 95 and Ada allow user-defined overloaded operators. Java does not permit operator overloading. Chapter 7: Expressions and Assignment Statements 15
16
Type Conversions Type conversions are either:
A narrowing conversion is one that converts an object to a type that cannot include all of the values of the original type e.g., float to int. May lose data. A widening conversion is one in which an object is converted to a type that can include at least approximations to all of the values of the original type e.g., int to float. Generally safe. Chapter 7: Expressions and Assignment Statements 16
17
Type Conversions (cont.)
Type conversions may be: Implicit (coercion). Explicit (such as casting in C). A mixed-mode expression is one that has operands of different types. A coercion is an implicit type conversion. In most languages, all numeric types are coerced in expressions, using widening conversions. In Java, byte and short are coerced to int whenever an operation is applied. Chapter 7: Expressions and Assignment Statements 17
18
Explicit Type Conversions
Called casting in C-based language. Examples: C conversion (cast) int a, b; float x; x = (float)a/(float)b; Chapter 7: Expressions and Assignment Statements 18
19
Type Conversions: Errors in Expressions
Causes: Errors resulting from narrowing conversions. Errors resulting from limitations of computer arithmetic: Overflow. Underflow. Some languages (Ada, C++, Java) provide an exception handling mechanism that allows the programmer to handle conditions such as divide by 0. Often ignored by the run-time system. Chapter 7: Expressions and Assignment Statements 19
20
Relational and Boolean Expressions
Relational operators in various languages: Ada: =, /=, >, <, >=, <= Java: ==, !=, >, <, >=, <= Relational operators always have lower precedence than arithmetic operators. Relational Expressions: Use relational operators and operands of various types. Evaluate to some Boolean representation. Boolean Expressions: Operands are Boolean and the result is Boolean. C/C++ operators: &&, ||, ! Chapter 7: Expressions and Assignment Statements 20
21
Relational and Boolean Expressions: No Boolean Type in C
C has no Boolean type, it uses int type with 0 for false and nonzero for true. Chapter 7: Expressions and Assignment Statements 21
22
Short-Circuit Evaluation
A short-circuit evaluation of an expression is one in which the result is determined without evaluating all of the operands and/or operators. Types: In Arithmetic Expressions: Ex.: (13*a) * (b/13–1) If a is zero, there is no need to evaluate (b/13-1). In logical Expressions: Ex.: The value of (a >= 0) and (b < 10) is independent of the second relational expression if a < 0. Chapter 7: Expressions and Assignment Statements 22
23
Short-Circuit Evaluation (cont.)
Lack of short circuit evaluation can cause problems: Index = 1; while ((Index < listlen) && (list[Index] != key)) Index = Index + 1; If the expression doesn’t have short-circuit, then both operands to “&&” are evaluated. C, C++, and Java: use short-circuit evaluation for the usual Boolean operators (&& and ||). Chapter 7: Expressions and Assignment Statements 23
24
Short-Circuit Evaluation (cont.)
Short-circuit evaluation exposes the problem of side effects in expressions: if ((a > b) || (b++/3)) If (a>b), the second expression which increments b is not evaluated. Ada has short-circuit logical operators: and then and or else if (index <= listlen) and then (list(index) /= key) Chapter 7: Expressions and Assignment Statements 24
25
Assignment Statements
The general syntax: <target_var> <assign_operator> <expression> The assignment operator: = FORTRAN, BASIC, PL/I, C, C++, Java. := ALGOLs, Pascal, Ada. Simple assignment: a = b; a = b = c; Suppose a, b, and c are integers. In C, the integer value of c is assigned to b, which is in turn assigned to a (multiple targets). Chapter 7: Expressions and Assignment Statements 25
26
Assignment statements
Multiple targets: Sum, Total = (PL/I) Sum = Total = 0 (C) Conditional targets: Perl allows it, for example: flag ? count1 : count2 = 0; Which is equivalent to: if (flag) count1 = 0; else count2 = 0; Compound assignment operators: a += b; // a = a + b; Chapter 7: Expressions and Assignment Statements 26
27
Assignment statements (cont.)
Unary assignment operators: count++; ++count; sum = ++count; count incremented then assigned to sum sum = count++; count assigned to sum then incremented Assignment as an expression: In C, C++, and Java the assignment statement produces a result and can be used as operands. while ((ch = getchar()) != EOF) { … } ch = getchar() is carried out; the result (assigned to ch) is used as a conditional value for the while statement. Chapter 7: Expressions and Assignment Statements 27
28
Mixed-Mode Assignment
Assignment statements can also be mixed-mode, for example: int a, b; float c; c = a / b; In Pascal, integer variables can be assigned to real variables, but real variables cannot be assigned to integers In Java, only widening assignment coercions are done. In Ada, there is no assignment coercion.
Similar presentations
© 2024 SlidePlayer.com Inc.
All rights reserved.