Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lecture 3 Expressions Richard Gesick.

Similar presentations


Presentation on theme: "Lecture 3 Expressions Richard Gesick."— Presentation transcript:

1 Lecture 3 Expressions Richard Gesick

2 Topics Expressions Data Conversion

3 Expression Combination of one or more operators and operands
Has both data type & value Operands may be literals constants variables

4 Arithmetic Operators Operator Operation + addition - subtraction *
multiplication / division % modulus (remainder after division)

5 Integer Division & Modulus
When dividing two integers: the quotient is an integer the remainder is truncated (discarded) To get the remainder, use the modulus operator with the same operands

6 Examples 8 25 / 3 25 % 3 3 / 25 3 % 25 25.0 / 5 10 / 3.0 1 3 5.0

7

8 Operator Precedence What is the order of evaluation in the following expressions? a + b + c + d + e a + b * c - d / e 1 2 3 4 3 1 4 2 a / (b + c) - d % e 2 1 4 3 a / (b * (c + (d - e))) 4 3 2 1

9 Assignment Revisited The assignment operator has a lower precedence than the arithmetic operators First the expression on the right hand side of the = operator is evaluated answer = sum / 4 + MAX * lowest; 4 1 3 2 Then the result is stored in the variable on the left hand side

10 Assignment Revisited First the expression on the right hand
side of the = operator is evaluated Expression has data type and value variable = expression; Then the result is stored in the variable on the left hand side (has data type), if types are compatible

11 Assignment Revisited The right and left hand sides of an assignment statement can contain the same variable First, one is added to the original value of count count = count + 1; Then the result is stored back into count (overwriting the original value)

12 Shortcut Operators ++ increment by 1 -- decrement by 1 Example:
count++; // count = count + 1; count--; // count = count - 1;

13 Increment and Decrement
The increment and decrement operators can be applied in postfix form: count++ or prefix form: ++count When used as part of a larger expression, the two forms can have different effects Because of their subtleties, the increment and decrement operators should be used with care

14 Which form to use? when the increment (or decrement) operator is used in a “stand alone” statement solely to add one (or subtract one) from a variable’s value, it can be used in either prefix or postfix form USE EITHER dogs-- ; dogs;

15 BUT... when the increment (or decrement) operator is used in a statement with other operators, the prefix and postfix forms can yield different results LET’S SEE HOW. . .

16 PREFIX FORM “First increment, then use ”
int alpha ; int num ; num = 13; alpha = ++num * 3; 13 num 14 alpha 42

17 POSTFIX FORM “Use, then increment ”
int alpha ; int num ; num = 13; alpha = num++ * 3; 13 num alpha 13 39 num alpha 14 num

18 More Shortcut Operators
Example Equivalent += a += 3; a = a + 3; -= a -= 10; a = a - 10; *= a *= 4; a = a * 4; /= a /= 7; a = a / 7; %= a %= 10; a = a % 10;

19 Assignment Operators is equivalent to
The right hand side of an assignment operator can be a complex expression The entire right-hand expression is evaluated first, then the result is combined with the original variable Therefore result /= (total-MIN) % num; is equivalent to result = result / ((total-MIN) % num);

20 Common Error Trap No spaces are allowed between the arithmetic operator and the equals sign Note that the correct sequence is +=, not =+ Example: add 2 to a // incorrect a =+ 2; // a = +2; assigns 2 to 2 // correct a += 2; // a = a + 2;

21 Assigning the Values of Other Variables
Syntax: dataType variable2 = variable1; Rules: 1. variable1 needs to be defined before this statement appears in the source code 2. variable1 and variable2 need to be compatible data types; in other words, the precision of variable1 must be lower than or equal to that of variable2.

22 Mixed-Type Arithmetic
When performing calculations with operands of different data types: Lower-precision operands are promoted to higher-precision data types, then the operation is performed Promotion is effective only for expression evaluation; not a permanent change Called "implicit type casting" Bottom line: any expression involving a floating-point operand will have a floating-point result.

23 Two types of data conversion
Widening conversion (promotion) safe, do not lose data goes to a “wider” (more bits) data type may lose precision (long to float) Narrowing conversion (demotion) may lose data may lose precision should be avoided compiler error unless specific cast done

24

25

26 Conversion Techniques
assignment conversion assign an int to a long promotion divide an int by a double casting

27 Explicit Type Casting Syntax: (dataType)( expression )
Note: parentheses around expression are optional if expression consists of 1 variable Useful for calculating averages double result = (double) 25 / 3; double result = (double) total / count;

28 Summary What did you learn?


Download ppt "Lecture 3 Expressions Richard Gesick."

Similar presentations


Ads by Google