You are on page 1of 25

Chapter 7 Expressions

and Assignment
Statements

Expression
Expression is the means of specifying
computations in programming language.

Arithmetic expression
Arithmetic expression is the programming
languages that inherited from mathematics
There are three types of operand:
Unary : contain single operand
Binary : contain two operand
Ternary : contain three operand

Example of Unary, Binary and


Ternary
Example of Unary:
X++,y - - C++,C, Java
Example of Binary:
x+y , y*z C++,Java, Python
Example of Ternary:
x=4 if b>8 else 9 Python

Operator Evaluation Order


Operator Evaluation Order are evaluated by the
Precedence and Associativity.

The Operator Precedence


The Operator Precedence is a rule that define
the order of the operators in different
precedence levels are evaluated.

Example of Operator
Precedence

The Operator Associativity


Operator Associativity is define the rule which the expression
has evaluated first when two operators has the same level of
precedence

Example of Operator
Associativity

Parentheses
Parentheses is used to override the precedence
and associativity rules by placing parentheses in
expressions.

Narrow Conversion
Narrow conversion is convert a value to a type
that cannot store
In others word is convert from big to small
For example: convert from double to int in C++

Widening conversion
A widening conversion is convert a value of the
original type that can include at least
approximations of all values for the original type.
In others word is convert from small to big
For example: converting a int to a double in C++

Mixed-mode
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

Type of conversion
There are two type of conversion:
Casts refer to explicit conversion
Coercion refer to inexplicit conversion

Example

Relational Expression
The relational expression has two operands and one
relational operator
The value of relational expression is Boolean
Example of relational operators :
==,!=,<=,<,>
Example of relational expression in C++:
int input;
(input <=1000)

Source code for relational


expression

Boolean expressions
Boolean expressions consists Boolean variable, Boolean
constants, relational expression and Boolean operators
The operators include AND(&&), OR(||) and NOT(!)
Example of Boolean Expression:
(i>0)&& (j<10), (i>0)||(j<100)

Source code for Boolean


Expression

Short Circuit Evaluation


Short circuit evaluation is the expression to
determine the result without evaluate all
operands or operators
Example:

Assignment statement
The assignment statement is using equal(=) for
assign the operands in the expressions.
The use of these operator must be different
from the equal sign to avoid the confusion
Example:

Source code for Assignment


statement

Compound assignment
The compound assignment operator is a
shorthand to specifying a common needed
form of assignment.
The method is assigning the first operand
in expression on the right side

Assignment as an Expression
Assignment as an Expression which produce the
result, has the same value assigned to the
target.

Multiple assignment
Multiple assignment is provide the multiple- target

You might also like