Saturday 4 April 2015

7.0 Expressions and Assignment Statements

Expressions are the fundamental means of specifying computations in a programming language.
The purpose of an assignment statement is to change the value of a variable.
In programming languages, arithmetic expressions consist of operators operands, parentheses and function calls.

An operator can be
Unary - meaning it has a single operand.      ie:   ! or ++ ,         ex:    int c  = x++
Binary - meaning it has two operands.          ie:   ==                  ex:    bool b  =     (c == d)
Ternary = Meaning it has three operands.     ie:   ?:                    ex:    (input > 0) ? "yes" : "no"

Unaray addition is called the identity operator because it usually has no associated operation and thus has no effect on its operand.

var a = 10
(-a).Dump();
 output = -10
yet variable a value still equals 10

7.2 Arithmetic Expressions

The purpose of an arithmetic expression is to specificity and arithmetic computation.
An implementation of such a computation must cause two actions . fetching the operands, usually from memory and executing the arithmetic operations on those operands.

7.2.1  Operator Evaluation Order

7.2.1.1 Precedence
- The order of evaluation of operators in the expression
- i.e.   a + b * c

- The operator precedence rules for expression evaluation define the order in which  the operator of difference precedence levels are evaluated. These are based on the hierarchy of operators priorities, as seen by the language designer.

7.2.1.2 Associativity 
When an expression contains two adjacent occurrences of operators with the same level of precedence, the question of which operator is evaluated first. This is answered by the associativity rules of the language.

i.e. a - b + c

An operator can have either  left to right associativity or right to left associativity

7.2.1.3 Parentheses
Programmers can alter the precedence and associativity rules by placing parentheses in expressions. A parenthesized part of an expression has precedence over its adjacent un-parenthesized parts. for example, although multiplication has precedence over addition, in the expression below, addition will be evaluated first.

i.e (A + B) * C


7.2.1.5 Conditional Expressions
We now look at the ternary operator ?: This operator is used to form conditional expressions.
Sometimes if-then-else statements are used to perform conditional expression assignment.


If (count == 0) Then {average = 0} Else {average = sum/ count)

average = (count == 0) ? 0 : sum/count


7.3 Overloaded Operators
The multiple use of an operator is called operator overloading and is generally thought to be acceptable, as long as readability and/or reliability don't suffer. 

Using the same symbol for two completely unrelated operations is detrimental to readability.

Risks are that a programmer could overload * to be an  addition operator. 

7.4 Type Conversions
-Type conversions are either narrowing or widening.

- A narrowing conversion converts a value to a type that cannot store even approximations of all of the values of the original type.  double to an int, Narrowing conversion are not always safe, sometimes the magnitude of the converted value is changed in the process.


 - A widening conversion converts a value to a type that can include at least approximations of all of the value of the original type. ie. int to a double, Widening conversion are nearly always safe, meaning that the magnitude of the converted value is maintained.


7.4.1 Coercion in  Expressions
- Implicit type conversion that is initiated by the compiler when an operator has two operands of a different type. With in the arithmetic expression  example below the compiler would simply insert code to coerce the value of the int operand to a double


int a = 10
double b = 5.5


var c = a * b

 Languages like java that don't allow such mixed mode expressions would just throw up a type error, and the programmer would have to do the conversion explicitly.


7.4.2 Explicit Type Conversion

Most languages provide some capability for doing explicit conversions, with widening and narrowing. In some cases warning messages are produced when an explicit narrowing conversion results in a significant change to the value of the object being converted.

In c-based languages, explicit type casts are called casts.

(double) sum


7.5 Relational and Boolean Expressions

7.5.1 Relational Expressions
- A relational operator is an operator that compares the values of it operands. ie. ==, !=
- A relational expression has two operands and one relational operator.
-  The result value of a relational expression is Boolean.
-checks the a type of relationship between operands

below are example of relational operators
==, !=,  >, <,  >=, <=

7.5.2  Boolean Expressions
- Boolean expressions consist of Boolean variables, Boolean constants, relational expressions and Boolean operator. The operator usual includes those for AND OR and NOT operations.

var a = (a.value == 10  &&   b.value == 10 )


7.6 Short-Circuit Evaluation
 A short-circuit evaluation of an expression is one in which the result is determined with evaluating all of the operands and/or operators. in the expression below

If (a >= 0) && (b<110)

If  the variable a evaluation fails to find that variable a is greater then or equal to  0 then we already know that we don't need to evaluate the second relational expression to know that this whole Boolean expression results is false.



7.7 Assignment Statement
-Provides the mechanism by which the user can dynamically change the binding of values to variables.
- <target_variable><assignment_operator><expression>

7.7.2 Conditional targets
 In C++

flag ? count 1 : count 2 = 0

equal to

if (flag) count1 = 0 else count 2 = 0

7.7.3 Compound Assignment Operators
 - A compound assignment operator is a shorthand method of specifying a commonly needed form of assignment.

ie.  sum +=  value is equivalent to     sum = sum + value 


7.7.4 Unary Assignment Operators
- operators with a single operand
- abbreviated assignments

example:      sum = count++;



 7.7.5 Assignment as an Expression
- an assignment expression within a relational expressions

IF ((c = a * b) ==100) 


No comments:

Post a Comment