Upcoming SlideShare
×

# 2 1 expressions

481 views

Published on

Published in: Technology
0 Likes
Statistics
Notes
• Full Name
Comment goes here.

Are you sure you want to Yes No
• Be the first to comment

• Be the first to like this

Views
Total views
481
On SlideShare
0
From Embeds
0
Number of Embeds
4
Actions
Shares
0
7
0
Likes
0
Embeds 0
No embeds

No notes for slide

### 2 1 expressions

1. 1. Arithmetic Expressions <ul><li>An expression is a combination of one or more operands and their operators . </li></ul><ul><li>Arithmetic operators: </li></ul><ul><ul><li>Addition </li></ul></ul><ul><ul><li>Subtraction </li></ul></ul><ul><ul><li>Negation </li></ul></ul><ul><ul><li>Multiplication </li></ul></ul><ul><ul><li>Division </li></ul></ul><ul><ul><li>Remainder (modulo) </li></ul></ul><ul><li>Operands can be variables, literals, calls, array elements, etc </li></ul>
2. 2. Examples <ul><li>Java </li></ul><ul><li>y = 3 + 4; </li></ul><ul><li>a = 2.7 / 3; </li></ul><ul><li>a = 1.5 / 0.5; </li></ul><ul><li>y = x + z; </li></ul><ul><li>y = x * 5; </li></ul><ul><li>a = pi * 2 * x; </li></ul><ul><li>a = x + 3 + foo(23); </li></ul><ul><li>n = m % 2 </li></ul><ul><li>Visual Basic </li></ul><ul><li>count = 3 + 4 </li></ul><ul><li>area = rad * rad * 3.14 </li></ul><ul><li>pints = quarts * 4 </li></ul><ul><li>mean = sum / count </li></ul><ul><li>six = one + two + 3 </li></ul><ul><li>even = number Mod 2 </li></ul>
3. 3. Operator Return Values <ul><li>We say an operator returns a value: </li></ul><ul><ul><li>4 * 2 returns 8 </li></ul></ul><ul><ul><li>3.14 – 1.0 returns 2.14 </li></ul></ul><ul><li>The return value from an expression can be: </li></ul><ul><ul><li>Assigned to a variable </li></ul></ul><ul><ul><li>Used in another expression </li></ul></ul><ul><ul><li>Passed to a method, function, or subroutine </li></ul></ul><ul><ul><li>Compared with another value </li></ul></ul><ul><ul><li>Etc. </li></ul></ul>
4. 4. Number Types <ul><li>If both operands are integers, the operator returns an integer, </li></ul><ul><li>Otherwise, if at least one operator is a double (or decimal), the operator returns a double </li></ul><ul><ul><li>4.5 * 2.5 returns 11.25 </li></ul></ul><ul><ul><li>3.14 – 2 returns 1.14 </li></ul></ul><ul><ul><li>4.0 – 2 returns 2.0 </li></ul></ul>
5. 5. Integer Division and Remainder <ul><li>Integer division returns the integer quotient of the two operators (the fractional part is discarded) </li></ul><ul><li>The remainder (modulo) operator (%) returns the integer remainder after dividing the first operand by the second </li></ul>14 / 3 returns 8 / 12 returns 4 0 14 % 3 returns 8 % 12 returns 2 8
6. 6. Operator Precedence (PEMDAS) <ul><li>Operators follow the algebraic order of operations </li></ul><ul><li>Multiplication, division, and remainder are evaluated prior to addition, subtraction, and string concatenation </li></ul><ul><li>Arithmetic operators with the same precedence are evaluated from left to right </li></ul><ul><li>Parentheses can be used to force the evaluation order </li></ul>
7. 7. Operator Precedence <ul><li>What is the order of evaluation in the following expressions? </li></ul>a + b + c + d + e 1 4 3 2 a + b * c - d / e 3 2 4 1 a / (b + c) - d % e 2 3 4 1 a / (b * (c + (d - e))) 4 1 2 3
8. 8. String Concatenation <ul><li>The string concatenation operator (+) connects a string to another data type </li></ul><ul><ul><li>E.g., “Hello, ” + “world!!” returns “Hello, world!!” </li></ul></ul><ul><ul><li>E.g., “Pi equals “ + 3.14159 returns “Pi equals 3.14159” </li></ul></ul><ul><li>If at least one operator is a string, the plus operator performs string concatenation </li></ul><ul><ul><li>E.g., “23“ + 1 returns “231” </li></ul></ul><ul><li>The + operator is evaluated left to right </li></ul><ul><li>Parentheses can be used to force the operation order </li></ul>
9. 9. Assignment Revisited <ul><li>The assignment operator has a lower precedence than the arithmetic operators </li></ul>answer = sum / 4 + MAX * lowest; 1 4 3 2 <ul><li>The right and left hand sides of an assignment statement can contain the same variable </li></ul>First, one is added to the original value of count Then the result is stored back into count (overwriting the original value) count = count + 1;
10. 10. Program statements <ul><li>All program statements end with a semicolon </li></ul><ul><ul><li>mean = total / count; </li></ul></ul><ul><li>Statements can span several lines: </li></ul><ul><ul><li>mean = (first + second + third) </li></ul></ul><ul><ul><ul><li> / 3; </li></ul></ul></ul><ul><li>One line can contain several statements; </li></ul><ul><ul><li>mean = total; mean = mean / count; </li></ul></ul>
11. 11. Data Conversions <ul><li>Sometimes it is convenient to convert data from one type to another </li></ul><ul><ul><li>For example, we may want to treat an integer as a floating point value during a division </li></ul></ul><ul><li>Conversions must be handled carefully to avoid losing information </li></ul><ul><ul><li>Widening conversions (e.g., int to double ) are safest because they usually do not lose information. </li></ul></ul><ul><ul><li>Narrowing conversions can lose information ( double to int ) </li></ul></ul>
12. 12. Data Conversions <ul><li>In Java, data conversions can occur in three ways: </li></ul><ul><ul><li>assignment conversion </li></ul></ul><ul><ul><li>arithmetic promotion </li></ul></ul><ul><ul><li>casting </li></ul></ul><ul><li>Assignment conversion occurs when a value of one type is assigned to a variable of another </li></ul><ul><ul><li>Only widening conversions can happen via assignment. </li></ul></ul><ul><ul><li>float waterBoils = 100; </li></ul></ul><ul><ul><li>int pi = 3.14159; is a syntax error </li></ul></ul>
13. 13. Arithmetic Promotion <ul><li>Arithmetic promotion happens automatically when operators in expressions convert their operands </li></ul><ul><ul><li>double circle = 3.14159 * 5; </li></ul></ul><ul><li>Java converts 5 to 5.0 to do the multiplication </li></ul>
14. 14. Data Conversions: Casting <ul><li>Casting allows widening and narrowing conversions </li></ul><ul><li>To cast, the type is put in parentheses in front of the value being converted: </li></ul><ul><ul><li>int pi = (int)3.14159; // Legal </li></ul></ul><ul><ul><li>int sum, count; </li></ul></ul><ul><ul><li>… </li></ul></ul><ul><ul><li>double average = (double) total / count; </li></ul></ul><ul><li>Converts total to a double value before evaluating the expression. </li></ul><ul><li>Why would we want to do this? </li></ul>
15. 15. Data Conversions: Strings to Numbers <ul><li>Strings are objects, so they do not cast or convert normally </li></ul><ul><ul><li>String foo = “53”, pi = “3.14156”; </li></ul></ul><ul><ul><li>int x = (int)foo; // Syntax error </li></ul></ul><ul><ul><li>double p = (double)pi; // Ditto </li></ul></ul><ul><li>Instead we have to use a method </li></ul><ul><ul><li>x = Integer.parseInt(foo); </li></ul></ul><ul><ul><li>p = Double.parseDouble(pi); </li></ul></ul>