You are on page 1of 11

TCP2651 Concept of Programming Language

Chapter 7 Expressions and Assignment Statements


Name: Peter Kong Kee Hieng
ID

: 1122700625

Chapter 7 Expressions and Assignment Statements


Expression is the means of specifying computations in programming language. A
formal mechanism (BNF) is used to describe the syntax of expressions. To get familiar
with the expression evaluation is to understand the order of the operator and operand
evaluation. The main issues in expression semantics must be take note is mismatches,
coercions, and short-circuit evaluation.
Arithmetic expression is the programming languages that inherited from
mathematics. In arithmetic expression consist of operators, operands, parentheses and
function calls. A unary only contain one single operand, binary contain two operands and
ternary contain three operands.
Operator Evaluation Order are evaluated by the Precedence and Associativity. The
Operator Precedence is a rule that define the order of the operators in different
precedence levels are evaluated.
Example for the following expression:
a+b*c=?

This multiplication has the priority than


the addition hence the multiplication

1+(2*3)=7

evaluated first

where a=1,b=2 and c=3

The exponential has the priority then the


additional.

(2^2)+4=8

The precedence of arithmetic operator of Ruby


Highest

**
Unary+,*,/,%
Binary +,-

Lowest

Operator Associativity is define the rule which the expression has evaluated first when
two operators has the same level of precedence. Associativity usually is left to right
except when the exponentiation operator is right to left.
3-2+1=2

The left operator is evaluated first

2^(2^2)=16

The right operator is evaluated first

Associativity Rule
*,/,+,-

Left

**

Right

*, /, %, binary +, binary Left


++, --, unary -, unary + Right
<, <=, >, >=

Left

==, !=

Left

&&, ||

Left

Parentheses is used to override the precedence and associativity rules by placing


parentheses in expressions. A parenthesized part of an expression has precedence over its
adjacent unparenthesized parts.

For example:
(A-B) *C
where A=4, B=2 and C= 4

In this case, the minus will carry first


because it is a parenthesized part

(4-2)*4=8
(2+2)+(3+3)
(4)+(6)=10

Both expression has parenthesized part is


carried out first before continue to
unparenthesized part

Conditional Expressions can be used to perform a conditional expression assignment as


the example as below: Conditional expressions can be used anywhere in a program
language for example Java, C, C++, Ruby and etc.
Example source code can be find in appendix section
Side Effect is the evaluation of one operand affect the value of the operand which is in
the same expression. Consider the following sample expression
Example
a+fun(a) where a=10

Explanation
Let a be 10 but the fun(a) there is an effect
and it will return 20

a=10;

If the a is run first the value will be 10 and

b=a+fun(a);

the value of the expression will be 20


whereas if the second fun(a) run first the
value will be 20 and the value of
expression will be 30

Functional Side Effect is a special type of side effect, where the evaluation of a function
affects the value of others operand the same expression. There are two possible solutions
for solving Operand Evaluation Order by allowing a function change to global variable or
its parameter

Example source code can be find in appendix section


Narrowing conversion is converts a value to a type that cannot store.
For example: converting a double to a float in C++.is a narrowing conversion because the
range of the float is larger than float.
A widening conversion is convert a value of the original type that can include at least
approximations of all values for the original type.
For example: converting a int to a double in C++
Mixed-mode expression is which mean the expression is different type of operands. If
both operands have different types of data types it is needed for the conversion of both
operands to same data types so that both operands have the same data type of operands.
Example
int a;

Explanation
Assume the a is int type but it is the mixed-

float b,c,d;

mode expression therefore a must change

d=a+b;

from int to float type.

Most of the programming language can do explicit conversions for both widening and
narrowing. There are two types of type conversion which are Casts and Coercion. Casts is
refer as the explicit conversions whereas the coercions is the inexplicit conversions.
Example
Coercion

Explanation
K is float and y is int therefore y need to

k+y

covert to the float to match with the data

Casts

type of k
A is the int and b is double data type

a=int(b)

therefore b need declare the data type to the


int in order to match with the a

Relational expressions has two operands and one relational operator. The value of a
relational expression is Boolean. The operation which to determine the truth or false of
the expression which depends on the operand types.

Example of the relational expression:


==,!=,<=,<,>

For example in C++ relational expression


int input;
(input <=1000)
Boolean expressions consists Boolean variables, Boolean constants, relational
expressions and Boolean operators. The operators include AND (&&), OR (||) and NOT
(!).
Example of the Boolean expression as below:
(i>0)&& (j<10), (i>0)||(j<100)
The arithmetic expression can be the operands of the relational expression and can be
operands of Boolean expressions. The precedence of arithmetic, relational and Boolean
operators in C languages as below:
Highest

postfix ++,-Unary +,-, prefix ++,--, !


Binary +,<,>,<=,>=
=,!=
&&

Lowest

||

Short circuit evalution is the expression to determine the result without evaluating all
operands or operators. For example as below:

Example
(5*7) * (13-1/a)

Explanation
If a is 0, because 12/a the result will be 0
and there is no need evaluate the 5*7

(a.>10) && (b<10)

If the a is 11 then the second expression no


need evaluated because True && False =
False

In Java,C and C++ programming language the && and || are short circuit but when & and
| are used they are no short circuit and they will use for the Boolean valued operands. But
for others programming language such as Ruby, Perl and Python they are short circuit.
Assignment statement is used the equal (=) sign for assign the operands in the expression.
The use of these operator must be different from the equal sign to avoid the confusion
with their assignment operator. Example as below:
Example
Char name = Ali;

Explanation
The variable of name is assign to character
of Ali

Compound assignment operator is a shorthand method to specifying a common needed


form of assignment. The method can be assigning the first operand in the expression on
the right side. As the example as below:
Example
Int a=0;

Explanation
During the beginning the value of a is 0

Int b=1;

and b is 1 when a+b which 0+1 =1 The

a=a+b

value of a is now become 1

Unary Assignment Operators combine increment and decrement operations with


assignment. The operator ++ is for increment and operator -- is for the decrement. As the
example below:
Example
int a=0;

Explanation
++a is a prefix add 1 to a and it will the

++a;

new value. At the beginning, the value a is


0 but after increment it will return 1

int a=0;

a++ is a postfix and it will return old value

a++;

after increment for example at the


beginning, the value of a is 0 after the
increment the value of a will be 0

count ++;

The example use of increment operator


which is equal to the count= count+1

Assignment as an Expression which produce the result, has the same value assigned to
the target. It can be used as an expression and operand in other expressions.
Example
If ((input=getchar()) !=EOF) {}

Explanation
The getchar() is assigned to the input get
string from keyboard input. Then the input
will compare with the EOF. If the input
does not match with the EOF therefore the
statement will not executed.

Multiple assignment is provide the multiple-target for example


Example
($a $b $c)=(10,20,30);

Explanation
In this example the a is assigned to 10, b is
assigned to 20 and c is assigned to 30 this
can be done using multiple assignment

Reference
1) ROBERT W.SEBESTA. (2012). Chapter 7 Expressions and Assignment
Statements (pp.317-345). CONCEPTS OF Programming Languages TENTH
EDITION.
2) What is the difference between ++i and i++(2008).Retrieved from
http://stackoverflow.com/questions/24853/what-is-the-difference-between-i-and-i
3) Difference between i++ and ++i in a loop? (2009). Retrieved from
http://stackoverflow.com/questions/484462/difference-between-i-and-i-in-a-loop
4) What is a side effect?(2011). Retrieved from
http://programmers.stackexchange.com/questions/40297/what-is-a-side-effect

Appendix

Source code for conditional expressions


if(salary>=3000)
count<<You are rich;
else
count<<You are poor;
Source code for functional side effect
Example changing global variable
int a =5;
int fun1(){
a=17;
return 3;
}
void fun2(){
a=a+fun1();
}
Void main(){
Fun2();
}
Example changing a parameters
if (count == 0)
average = 0;
else
average = sum / count;

You might also like