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
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
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.
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
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
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
Operator Precedence What is the order of evaluation in the following expressions? 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
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
Assignment Revisited The assignment operator has a lower precedence than the arithmetic operators answer  =  sum / 4 + MAX * lowest; 1 4 3 2 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 Then the result is stored back into count (overwriting the original value) count  =  count + 1;
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;
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 )
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
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
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; // Legal int sum, count; … double average = (double) total / count; Converts  total  to a double value before evaluating the expression. Why would we want to do this?
Data Conversions: Strings to Numbers Strings are objects, so they do not cast or convert normally String foo = “53”, pi = “3.14156”; int x = (int)foo;   // Syntax error double p = (double)pi; // Ditto Instead we have to use a method x = Integer.parseInt(foo); p = Double.parseDouble(pi);

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
  • 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
  • 3.
    Operator Return ValuesWe 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.
  • 4.
    Number Types Ifboth 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
  • 5.
    Integer Division andRemainder 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
  • 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
  • 7.
    Operator Precedence Whatis the order of evaluation in the following expressions? 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.
    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
  • 9.
    Assignment Revisited Theassignment operator has a lower precedence than the arithmetic operators answer = sum / 4 + MAX * lowest; 1 4 3 2 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 Then the result is stored back into count (overwriting the original value) count = count + 1;
  • 10.
    Program statements Allprogram 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;
  • 11.
    Data Conversions Sometimesit 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 )
  • 12.
    Data Conversions InJava, 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
  • 13.
    Arithmetic Promotion Arithmeticpromotion happens automatically when operators in expressions convert their operands double circle = 3.14159 * 5; Java converts 5 to 5.0 to do the multiplication
  • 14.
    Data Conversions: CastingCasting 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; // Legal int sum, count; … double average = (double) total / count; Converts total to a double value before evaluating the expression. Why would we want to do this?
  • 15.
    Data Conversions: Stringsto Numbers Strings are objects, so they do not cast or convert normally String foo = “53”, pi = “3.14156”; int x = (int)foo; // Syntax error double p = (double)pi; // Ditto Instead we have to use a method x = Integer.parseInt(foo); p = Double.parseDouble(pi);