Arithmetic Expressions

  • consist of operators, operands and parentheses

Operators

  • can be infix, prefix, or postfix (most of them are infix)

    1. Unary Operator: has one operator

      • i++, --i
    2. Binary Operator: has two operators

      • a + b
    3. Ternary Operator: has three operators

      • a ? b : c
  1. Operator Precedence Rules

    • determines the order in which operators are evaluated in an expression
    • Typical precedence levels:
      1. Parentheses ()
      2. Exponentiation ** or ^
      3. Multiplication *, /
      4. Addition & Subtraction + & -
  2. Operator Associativity Rule

    • refers to the order in which operators with the same precedence are evaluated in an expression
    • Typical associativity rules:
      • right to left (except exponentiation **)
        1
        2 ** 2 ** 3 # 256
    • Remind: APL is different: all operators have equal precedence and all operators associate right to left
      1
      2 * 5 + 7   ⍝ 24

Side Effecr

  • it occurs when a function changes a two-way parameter, a non-local variable, or a static-storage variable

    1. updated a non-local variable

      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      int fun(int& x){
      x += 1;
      return x * 2;
      }

      int main(){
      int a = 0;
      cout << a + fun(a) << endl; // 3 (expected: 2)
      int b = 0;
      cout << b + 1 + fun(b) << endl; // 3 (the expect value is not previous + 1)
      return 0;
      }
    2. updated a static-storage variable

      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      int g = 1;

      int fun(int b){
      g += 1;
      return 2 * b * g;
      }

      int main(){
      cout << fun(2) << endl; // 8
      cout << fun(2) << endl; // 12 (the expect value is not same as previous)
      return 0;
      }
  • Solutions

    1. disallow functional side effects
      • No two-way parameters in functions
      • No static-storage / non-local assignable variable access in function
      • Disadvantages: Inflexibility

Referential Transparency

  • if it always produces the same result for a given set of input parameters, and its result does not depend on any external state or side effects.

    • the function is referential transparency if result1 and result2 are equivalent
      1
      2
      3
      result1 = (fun(a) + b) / (fun(a) - c)
      temp = fun(a)
      result2 = (temp + b) / (temp - c)
  • Advantages

    • much easier to understand as they do not have variables

Overloaded Operators

  • an operator can work for more than one data type or class

    • 1 + 2: work for int
    • 1.1 + 2.2: also work for float
    • &a: getting the address of a
    • a&b: logic and operation
  • C++, C#, and F# allow user-defined overloaded operators

    • When sensibly used, such operators can be an aid to readability
    • Potential problems:

      1. users can define nonsense operations
      2. readability may suffer, even when the operators make sense

        1
        2
        3
        4
        5
        6
        7
        8
        9
        10
        11
        12
        13
        14
        15
        16
        17
        18
        19
        20
        21
        22
        23
        24
        25
        26
        27
        28
        #include <iostream>
        using std::cout;
        using std::endl;
        using std::ostream;

        class Vector{
        public:
        Vector(float x, float y, float z) : x(x), y(y), z(z) { }
        Vector operator+(const Vector& another){
        return Vector(x+another.x, y+another.y, z+another.z);
        }
        float getX() { return x; }
        float getY() { return y; }
        float getZ() { return z; }
        private:
        float x, y, z;
        };

        ostream& operator<<(ostream& os, Vector v){
        os << "(" << v.getX() << "," << v.getY() << "," << v.getZ() << ")";
        return os;
        }

        int main(){
        Vector a(1,2,3), b(2,4,6);
        Vector c = a + b;
        cout << a << " + " << b << " = " << c << endl; return 0;
        }

Type Conversions

  1. narrowing conversion (Not allow in Java)

    • the converted object cannot include all of the values of the original type by the programming language
    • float to int
  2. widening conversion

    • the converted object can include at least approximations to all of the values of the original type
    • int to float

Implicit Type Conversions

  • automatic conversion of a value from one data type to another
    1
    2
    3
    4
    a = 42
    b = 3.14
    c = a + b
    print(c) # 45.14 (a is automatically converted to float)
  • Advantage: convenience
  • Disadvantage: reliability (lower the error detection ability of the compiler)

Explicit Type Conversions

  • manual conversion of a value from one data type to another by the programmer
  • int(variable) or (int)variable: depend on language
    1
    2
    3
    a = "42"
    b = float(a)
    print(b) # 42.0
    1
    2
    3
    double c = 3.14;
    int d = (int) c;
    System.out.println(d); // 3

Relational and Boolean Expressions

Relational Expressions

  • the comparation of two operands with one relational operator and evaluate to Boolean
  • some operator symbols which vary among languages (!=, ne, <>)
  • JavaScript and PHP have two additional relational operator === and !==
    1
    2
    3
    4
    5
    6
    1 == 1.0    // true
    1 == '1' // true
    1 == true // true
    1 === 1.0 // true (javascript only has number type)
    1 === '1' // false
    1 === true // false

Boolean Expressions

  • The operands and the result are all Boolean

    • C, C++, Java: &&, ||
    • Python: and, or
  • Traditional C: Boolean true value is 1 whereas Boolean false value is 0

  • Chain comparison:

    1
    bool re = 3 > 2 > 1;    // false
    1
    re = 3 > 2 > 1     # True

Short-Circuit Evaluation

  • an expression in which the result is determined without evaluating all of the operands and/or operators

    • (87 * a) * (b / 17 - 9): if a is 0, then no need to evaluate (b / 17 - 9)
    • A && B: if A is false, then no need to evaluate B
    • A || B: if A is true, then no need to evaluate B
  • bitwise Boolean operators that are not short-circuit: & and |

  • Advantages: efficiency (save the compilation power)

  • Condition: program correctness

    • if we use the & operator that doest not has short-circult
      1
      2
      3
      4
      5
      6
      7
      8
      public static void main(String[] args){
      int[] a = {1,2,3};
      int index = 0;
      while(index < a.length & a[index] != 1337){
      index++;
      }
      System.out.println("test");
      }
    • it is compilation error, because when index is 4, a[index] does not exist
  • Potential error: (a > b) || (b++ / 5) if (a > b) is true, b++ does not be executed

Assignment Statements

  • General Syntax: <target_variable> <assignment_operator> <expression>
  • Assignment Operators

    • Most of languages: =
    • Pascal and Ada: :=
  • Conditional Targets

    1
    2
    3
    4
    5
    6
    7
    8
    9
    ($dog_lover ? $dog_is_da_best : $cat_is_da_best) = 1

    # is equivalent to

    if ($dog_lover) {
    $dog_is_da_best = 1
    } else {
    $cat_is_da_best = 1
    }
  • Compound Assignment Operators

    • a shorthand method of specifying a commonly needed form of assignment
      • a = a + b can be simply written as a += b
  • Multiple Assignments

    • Python, Perl, and Ruby allow multiple-target multiple-source assignments
      • a, b, c = 1, 2, 3: assign 1, 2, 3 to a, b, c
      • a, b = b, a: swap a, b
  • Mixed-Mode Assignment

    • allows values of different types to be assigned to each other
      1
      2
      3
      4
      int x = 5;
      double y = 3.14;
      x = y; // y is implicitly converted to int, x = 3
      y = x; // x is implicitly converted to double, y = 5.0