+ All Categories
Home > Technology > 2 1 expressions

2 1 expressions

Date post: 18-Dec-2014
Category:
Upload: ken-kretsch
View: 285 times
Download: 2 times
Share this document with a friend
Description:
 
15
1 Arithmetic Expressions • An expression is a combination of one or more operands and their operators. Arithmetic operators: • Addition • Subtraction • Negation • Multiplication • Division Remainder (modulo) Operands can be variables, literals, calls, array elements, etc
Transcript
Page 1: 2 1  expressions

1

Arithmetic Expressions

• An expression is a combination of one or more operands and their operators.

• Arithmetic operators:• Addition• Subtraction• Negation• Multiplication• Division• Remainder (modulo)

• Operands can be variables, literals, calls, array elements, etc

Page 2: 2 1  expressions

2

Examples

• Java

y = 3 + 4;

a = 2.7 / 3;

a = 1.5 / 0.5;

y = x + z;

y = x * 5;

a = pi * 2 * x;

a = x + 3 + foo(23);

n = m % 2

• Visual Basic

count = 3 + 4

area = rad * rad * 3.14

pints = quarts * 4

mean = sum / count

six = one + two + 3

even = number Mod 2

Page 3: 2 1  expressions

3

Operator Return Values

• We say an operator returns a value:

4 * 2 returns 8

3.14 – 1.0 returns 2.14• The return value from an expression can be:

– Assigned to a variable

– Used in another expression

– Passed to a method, function, or subroutine

– Compared with another value

– Etc.

Page 4: 2 1  expressions

4

Number Types

• If both operands are integers, the operator returns an integer,

• Otherwise, if at least one operator is a double (or decimal), the operator returns a double

4.5 * 2.5 returns 11.25

3.14 – 2 returns 1.14

4.0 – 2 returns 2.0

Page 5: 2 1  expressions

5

Integer Division and Remainder

• Integer division returns the integer quotient of the two operators (the fractional part is discarded)

• The remainder (modulo) operator (%) returns the integer remainder after dividing the first operand by the second

14 / 3 returns

8 / 12 returns

4

0

14 % 3 returns

8 % 12 returns

2

8

Page 6: 2 1  expressions

6

Operator Precedence (PEMDAS)

• Operators follow the algebraic order of operations

• Multiplication, division, and remainder are evaluated prior to addition, subtraction, and string concatenation

• Arithmetic operators with the same precedence are evaluated from left to right

• Parentheses can be used to force the evaluation order

Page 7: 2 1  expressions

7

Operator Precedence

• What is the order of evaluation in the following expressions?

a + b + c + d + e1 432

a + b * c - d / e3 241

a / (b + c) - d % e2 341

a / (b * (c + (d - e)))4 123

Page 8: 2 1  expressions

8

String Concatenation

• The string concatenation operator (+) connects a string to another data type

– E.g., “Hello, ” + “world!!” returns “Hello, world!!”

– E.g., “Pi equals “ + 3.14159 returns “Pi equals 3.14159”

• If at least one operator is a string, the plus operator performs string concatenation

– E.g., “23“ + 1 returns “231”

• The + operator is evaluated left to right

• Parentheses can be used to force the operation order

Page 9: 2 1  expressions

9

Assignment Revisited

• The assignment operator has a lower precedence than the arithmetic operators

answer = sum / 4 + MAX * lowest;

14 3 2

• The right and left hand sides of an assignment statement can contain the same variable

First, one is added to theoriginal value of count

Then the result is stored back into count (overwriting the original value)

count = count + 1;

Page 10: 2 1  expressions

10

Program statements

• All program statements end with a semicolon

mean = total / count;• Statements can span several lines:

mean = (first + second + third)

/ 3;• One line can contain several statements;

mean = total; mean = mean / count;

Page 11: 2 1  expressions

11

Data Conversions

• Sometimes it is convenient to convert data from one type to another

– For example, we may want to treat an integer as a floating point value during a division

• Conversions must be handled carefully to avoid losing information

– Widening conversions (e.g., int to double) are safest because they usually do not lose information.

– Narrowing conversions can lose information (double to int)

Page 12: 2 1  expressions

12

Data Conversions

• In Java, data conversions can occur in three ways:– assignment conversion

– arithmetic promotion

– casting

• Assignment conversion occurs when a value of one type is assigned to a variable of another– Only widening conversions can happen via assignment.– float waterBoils = 100;– int pi = 3.14159; is a syntax error

Page 13: 2 1  expressions

13

Arithmetic Promotion

• Arithmetic promotion happens automatically when operators in expressions convert their operands

double circle = 3.14159 * 5;

• Java converts 5 to 5.0 to do the multiplication

Page 14: 2 1  expressions

14

Data Conversions: Casting

• Casting allows widening and narrowing conversions• To cast, the type is put in parentheses in front of the

value being converted:int pi = (int)3.14159; // Legalint sum, count;…double average = (double) total / count;

• Converts total to a double value before evaluating the expression.

• Why would we want to do this?

Page 15: 2 1  expressions

15

Data Conversions: Strings to Numbers

• Strings are objects, so they do not cast or convert normallyString foo = “53”, pi = “3.14156”;

int x = (int)foo; // Syntax error

double p = (double)pi; // Ditto

• Instead we have to use a methodx = Integer.parseInt(foo);

p = Double.parseDouble(pi);


Recommended