Sebesta Chapter 7
Expressions and the Assignment Statement

Arithmetic Expressions
Overloaded Operators
Coercion in Expressions
Relational and Boolean Expressions
Short-Circuit Evaluation
The Assignment Statement
Mixed Mode Assignment

Arithmetic Expressions
Characteristics inherited from math
Includes combinations of operators, operands, parentheses and 
function calls

unary  (monadic)

binary (dyadic)

parameterless (niladic)




Purpose -- to specify an arithmetic computation, involve 
evaluation of operand specs and execution of operations
      Operator Evaluation Order
            Precedence
               unary in Ada        -A  legal        B + - A is not
                  none in APL
            
            Associativity
                  rules for adjacent operators of the same precedence

            Parentheses
                  overrides precedence

      Operand Evaluation Order  
      problems in timing -- e.g. when expression has a function 
             call




            Side Effect  (functional side effect)   occurs when  
the function changes one of its parameters or changes some other 
variable declared outside the function

                  A + FUN (A)

procedure sub1(...);
      var A : integer;
      function FUN(X : integer):integer;
            ...
            A := 17;
            ...
      end; {FUN}
      procedure sub2 (...);
            ....
            A := A + FUN (B);
            ...
      end;  {sub2}
end {sub1}


            Overcome problem:

            1.  Disallow functional sideeffects
            2. Operands to be evaluated in given order
            3. Functions in FORTRAN 77 legal only if don't change 
                  other values in expression

Overloaded Operators
      multiple use of operators
            & in C binary bitwise AND
                  unary address  of the variable
            + in BASIC
            Ada  allows FUNCTION "*" definition will decide on 
                  basis of operands


Coercion in Expressions
      implicit type conversion initiated by compiler

      mixed mode expressions

      float and short int always forced to double and int in 
            expressions or actuals

      not good allows errors

      Ada only allows mixed for **

            Avg := Real (Sum) / Real(count)

      Too much flexibility also costly --   PL/I strings as floats


Relational and Boolean Expressions

      Relational Expressions
            relational operator  compares values of two operands 
              and returns indication  of whether the operator 
              holds or does not
            relational expression  has two operands and one 
                    operator


OPERATION      PASCAL      ADA      C      FORTRAN      
equal            =            =      ==      .EQ.            
not equal      <>            /=      !=      .NE.            
greater than      >            >      >      .GT.      

relational operators usually have lower precedence than arithmetic  
      -- but not APL
      

      Boolean Expressions
            FORTRAN 77  .AND.  .OR.  .NOT.
            C            &&      ||      !

            Ada's Booleans at same precedence (except not)
                  more than one Boolean, must parenthesize

            C      has no Boolean type
                  returns 0 for false and 1 if true


Short-Circuit Evaluation
      result is determined without evaluating all of the operands 
           and/or operators

      A * B/C   

      (A >= 0) and (B < 10)

      problem if doesn't short circuit
            INDEX := 1;
            while (Index <= length) and (List[Index] <> Value) do
                  Index := Index + 1;

      Ada solution 
           INDEX := 1;
           while (Index <= length) and then (List(Index) <> Value)
                  loop
                  Index := Index + 1;
                  end loop;

      C and Modula-2   every AND and OR expression short circuit


The Assignment Statement  central construct of imperative 
languages

      Simple Assignment
        
            FORTRAN, BASIC, PL/I  C  use =
            PL/I  A = B = C   (true or false)                  ALGOL60  :=
            Pascal, Ada  stand alone statement

      Multiple Targets
            PL/I    A, B = 0

      Conditional Expressions
            Pascal             if (count = 0)
                            then average := 0
                            else average := sum / count

            C            average = (count == 0) ? 0 : sum / count
                  where ? acts as then       and : acts as else


      Conditional Targets
            not readable
            ALGOL 68      (link  = nil | next | link) := nil
            Pascal            if link = nil then next := nil
                                    else link := nil;



      Assigning Operators
            short hand  destination variable also as first operand 
               of expression on right
            PL/I      a = a + b
            C      a += b  {most of binary operators have}

      Unary Operator Assignments (aabreviated assignments)
      C      came from hardware of PDP-11
            sum = ++count
                  count = count + 1
                  sum = count
            sum = count ++
                  sum = count
                  count = count + 1
            count ++


      Assignment Statements as Operands
            statement produces a result, result assigned to a 
               target
            C
                  while ((ch = getchar () != EOF)
                        {       ...  }
                  note assignment has lower precedence in C than 
                   relational operators

Mixed Mode Assignment
      FORTRAN uses same rules as for mixed mode
      Pascal  some
      Ada and Modula-2  no coercion of integer to float
                        allow some subranges
********End of notes