CHAPTER 03
EXPRESSIONS, OPERATORS & ASSIGNMENTS
EXPRESSIONS & OPERANDS
Expression is a sequence of operands and operators that reduces to single value
2 + 3 * 5 3 + 4
5.6 + 6.2 * 3 operand operator operand
x + 2 * 5 + 6 / y
Operator is a language specific syntactical token that requires an action to be taken
Operands receive an Operators action
Operators might require one, two or more operands
There is no limit on the number of operators and operands used to form an
expression
2
ARITHMETIC OPERATORS
Most familiar operators are drawn from mathematics
Operation Operator Allowed Types
Multiplication * Any
Division / Any
Subtraction - Any
Addition + Any
Modulus % integer types
ORDER OF PRECEDENCE
The precedence rules of arithmetic operators are:
*, /, %
are at a higher level of precedence than
+ , -
Operators *, /, and % have the same level of precedence.
Operators + and - have the same level of precedence.
When operators are all on the same level, they are performed from left to right.
To avoid confusion, the grouping symbol can be used.
4
ORDER OF PRECEDENCE
Example
3*7-6+2*5/4+6 means
(3 * 7) - 6 + ( ( 2 * 5) / 4 ) + 6
= 21 - 6 + (10 / 4) + 6 (Evaluate *)
= 21 6 + 2 + 6 (Evaluate /)
= 15 + 2 + 6 (Evaluate -)
= 17 + 6 (Evaluate first +)
= 23 (Evaluate +)
5
COMPOUND ASSIGNMENT
x *= y (x = x * y)
x /= y (x = x / y)
x %= y (x = x % y) int main ()
x += y (x = x + y) {
x -= y (x = x - y) int x=5;
x /= y + 3 (x = x / (y+3)) int y=4;
Compound
x*=y;
assignment
cout<<x<<endl;
return 0;
}
6
INCREMENT AND DECREMENT OPERATORS
Increment operator - increment the value of a variable by 1
Decrement operator- decrement the value of a variable by 1.
Pre Increment: ++variable ++count; or count++;
Post Increment: variable++ increments the value of count by 1.
Pre Decrement: --variable --count; or count--;
Post Decrement: variable-- decrements the value of count by 1.
Suppose a and b are int variables.
a = 5;
b = 2 + (++a);
After the second statement a is 6 and b is 8.
7
OPERATORS
Operators are symbols representing operations that can be
performed on variable and constants. There are four operations
available in C++ programming.
1. Assignment Operations
2. Arithmetic Operations
3. Relational Operations
4. Logical Operations
8
ASSIGNMENT OPERATORS
The Assignment operator is denoted by the equal sign ( = )
It Is used to store/assign a value to variable
Example:
a = 5;
9
ARITHMETIC OPERATORS
10
RELATIONAL OPERATORS
11
LOGICAL OPERATOR
And
true and true true
true and false false
In c++ : &&
Or
true or false true
false or false false
In c++ ||
Not
not true false
In c++ !
12
LOGICAL OPERATOR - USAGE
int a = 4, b = 0;
a && b
a || b
!a
!b
(a+b)
(a-4)
13
LOGICAL OPERATORS TRUTH TABLE
14
EXPRESSIONS
A sequence of operands and operators that reduces to a single
value
Example:
Operand
2+5 Expression
Operator
15
C++ EXPRESSION FORMAT
16
PRIMARY EXPRESSIONS
Three types of primary expressions
17
BINARY EXPRESSIONS
18
BINARY EXPRESSION
-- MULTIPLICATIVE EXPRESSIONS
10 * 12
20 / 4
5 % 2 ???
Modulus Operator (%)
5 % 2 1
5 % 3 2
6 % 3 0
19
BINARY EXPRESSION
-- ADDITIVE EXPRESSIONS
Additive expression
3 + 5, 4 6
20
ASSIGNMENT EXPRESSIONS
The assignment expression has a value and a result.
Value: the value of the expression on the right of the assignment operator (=).
Result: the result copies the expression value to the left of the assignment operator.
The left operand in an assignment expression must be a single variable.
21
SIMPLE ASSIGNMENT
Consists of simple algebraic expressions
Examples
22
COMPOUND ASSIGNMENT
Shorthand notation for a simple assignment
Examples
23
SAMPLE CODE FOR ASSIGNMENT EXPRESSION
24
POSTFIX EXPRESSIONS
Remember!
(a++) is (a = a + 1)
(a ) is (a = a 1)
25
UNARY EXPRESSIONS
Remember!
(++a) is (a = a + 1)
( a) is (a = a 1)
26
OPERATOR PRECEDENCE EXAMPLES
2+3*4
( 2 + ( 3 * 4) )
-b++
( -( b++ ) )
27
OPERATOR ASSOCIATIVITY
Determine the
evaluation order
of the operators having
the same precedence
Left associativity vs.
Right associativity
28
OPERATOR ASSOCIATIVITY EXAMPLES
Left associativity
Right associativity
29
30
STATEMENTS
A block of instructions
Types of statements
31
EXPRESSION STATEMENTS
Examples
a = 2;
a = b = 3;
a = 4 + 5;
a = b + (45 / c) + 22;
a++;
* An expression statement is terminated with a semicolon (;). The
semicolon is a terminator, and it tells the compiler that the
statement is finished.
32
COMPOUND STATEMENTS
A block of multiple statements
33
SAMPLE PROGRAMS
34
Sample Programs
35
SAMPLE PROGRAMS
36
SAMPLE PROGRAMS
37
38
39
40
41
UNARY OPERATORS
Require only ONE operand / value
Used to express the POSITIVITY / NEGATIVITY of the numeric
data
UNARY +
UNARY
EXAMPLE to express freezing temperature: -17
BINARY OPERATORS
Require TWO operands / values
ADDITION ( + )
SUBTRACTION ( )
MULTIPLICATION ( * )
DIVISION ( / ) BOTH FOR INTEGER AND FLOAT
MODULUS ( % ) FOR REMAINDER
OPERATORS PRECEDENCE [01]
Highest Level Priority Unary +, Unary
Middle Level Priority Multiplication (*), Division (/), Modulus
(%)
Lowest Level Priority Addition (+), Subtraction(-)
Precedence of Operators can be changed by the use of
BRACKETS
OPERATORS PRECEDENCE [02]
For the following declarations
int num_1;
int num_2;
int num_3;
int answer;
In the following assignment statement
answer = num_1 + num_2 / num_3;
Division will be performed FIRST than Addition
USE OF BRACKETS TO CHANGE PRECEDENCE
BRACKETS can be used to change the Precedence of Operators
For the same declarations, in the following assignment statement:
answer = (num_1 + num_2) / num_3;
Addition will be performed first than the Division
OPERATORS ASSOCIATION
When the Operators have same Precedence then their
ASSOCIATIVITY is from Left to Right
For the same declarations, in the following assignment statement
answer = num_1 num_2 + num_3;
Subtraction will be performed first than the Addition
ASSOCIATIVITY RULES
Most C# arithmetic operators are left associative, within the same
precedence level
a / b * c equals (a / b) * c
a + b - c + d equals ( ( a + b ) - c ) + d
C# also has a few operators that are right associative.
EXAMPLE
PRECEDENCE AND
ASSOCIATIVITY
Mathematical formula:
________
- b + b2 - 4 a c
----------------------
2a
C# formula:
(- b + sqrt ( b * b - 4.0 * a * c) ) / ( 2.0 * a )
CLASS ACTIVITY
FOLLOWING TABLE SHOWS ALL THE ARITHMETIC OPERATORS SUPPORTED BY C#. ASSUME
VARIABLE A HOLDS 10 AND VARIABLE B HOLDS 20, THEN:
30
-10
200
2
++A (pre fix)11, A++ (post
fix) 10 , same for B.
--A (pre fix)9, A-- (post fix)
10 , same for B.
51