Expressions & Conversions L3

advertisement

Lecture 3

Expressions

Richard Gesick

Figures from Lewis, “C# Software Solutions”, Addison Wesley

CSE 1301

• Expressions

• Data Conversion

Topics

CSE 1301

Expression

• Combination of one or more operators and operands

• Has both data type & value

• Operands may be

– literals

– constants

– variables

CSE 1301

CSE 1301

Arithmetic Operators

Operator Operation

/

*

-

+

% addition subtraction multiplication division modulus

(remainder after division)

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

CSE 1301

CSE 1301

• 25 / 3

• 25 % 3

• 3 / 25

• 3 % 25

• 25.0 / 5

• 10 / 3.0

Examples

8

1

0

3

5.0

3.33333

CSE 1301

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

CSE 1301

CSE 1301

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

CSE 1301

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 if types are compatible data type ),

CSE 1301

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)

CSE 1301

Shortcut Operators

++ increment by 1

-decrement by 1

Example: count++; // count = count + 1; count--; // count = count - 1;

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

CSE 1301

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;

CSE 1301

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. . .

CSE 1301

CSE 1301

PREFIX FORM

“First increment, then use ” int alpha ; int num ;

13 num alpha num = 13;

14 num alpha = ++num * 3;

14 42 num alpha

CSE 1301

POSTFIX FORM

“Use, then increment ” int alpha ; int num ;

13 num alpha num = 13;

13 39 alpha alpha = num++ * 3; num

14 num

CSE 1301

More Shortcut Operators

Operator Example Equivalent

+=

-=

*=

/=

%= a += 3; a = a + 3; a -= 10; a = a - 10; a *= 4; a /= 7; a = a * 4; a = a / 7; a %= 10; a = a % 10;

Assignment Operators

• 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);

CSE 1301

CSE 1301

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;

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.

CSE 1301

CSE 1301

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 floatingpoint result.

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

CSE 1301

CSE 1301

CSE 1301

Conversion Techniques

• assignment conversion

– assign an int to a long

• promotion

– divide an int by a double

• casting

CSE 1301

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;

CSE 1301

CSE 1301

Summary

• What did you learn?

Download