Chap04 - Operators Expressions
Chap04 - Operators Expressions
Chapter 4
Operators and Expressions
Objectives
• Operators and Expressions
− Understand and use the arithmetic, relational and
logical operators.
− Understand and use the assignment operators
and expressions.
Expressions
• E.g. expression: 3 + 6 - 4
Operand
• Arithmetic operators
• Assignment operators
• Increment and decrement operators
• Relational operators
• Logical operators
• Ternary conditional operators
Arithmetic Operators
C++ Operation Arithmetic C++ Expression
operator
Addition + 5 + 2 is 7
5.0 + 2.0 is 7.0
Subtraction - 5 – 2 is 3
5.0 – 2.0 is 3.0
Multiplication * 5 * 2 is 10
5.0 * 2.0 is 10.0
Division / 5.0 / 2.0 is 2.5
5 / 2 is 2
Avoid
Modulus (remainder) % 5 % 2 is 1 Integer Division
To prevent integer division:
Example 1:
double comm,sales;
sales = 1000.00;
comm = 6/100*sales; comm = 6/100.0*sales;
x -= y x=x-y
x *= y x=x*y
x /= y x=x/y
x %= y x=x%y
x *= y+1 x = x * (y+1)
Precedence and Associativity
• Example
⮚ (2 + 3) * 4 = 20
⮚ 2 + 3 * 4 = 14
⮚ 3*8%5/2=2
⮚ 10>90 && 5<8 || 1 = 1
Types of Associativity
a) Left
associativity:
It evaluates the expression by
starting on the left and moving to the
b)
right Right
associativity:
It evaluates the expression by
proceeding from the right to the left
Left Associativity
3*8/4%4*5
Right Associativity
a += b *= c -= 5
a=a+b=b*c=c-
5
Types of Associativity - Example
■So,
a= a+ (b= b* (c= c-5 )))
a= 3+ (b=(5* (c= 8-5 )))
=18
Operators
precedenc
e and
associativit
y in C++
Increment & Decrement Operators
• Increment operator (++):
- increment the value of a variable by 1
• Decrement operator (--):
- decrement the value of a variable by 1
• Prefix & Postfix expression:
✔ Prefix-increment: ++variable
✔ Postfix-increment: variable++
✔ Prefix-decrement: --variable
✔ Postfix-decrement: variable--
Increment & Decrement Operators
Note :
• Prefix expression:
✔ The effect of increment/decrement is seen when
the current statement is evaluated
• Postfix expression:
✔ The effect of increment/decrement is seen after
the current statement is evaluated
Prefix Expression
1. int a, b = 5, c, d = 10;
c = ++d;
a = --b;
cout << “a= ” << a << “, b= ” << b << “, c = ” <<
c << “, d = ” << d;
2. a = 5;
b = 10;
cout << “a= ” << a << “, b= ” << b << endl;
cout <<“++a = ” << ++a << “--b= ” << --b << endl;
cout << “a= ” << a << “, b= ” << b << endl;
a=5, b=10
++a=6, --b=9
a=6, b=9
Postfix Expression
1. int a, b = 5, c, d = 10;
c = d++;
a = b --;
cout << “a= ” << a << “, b= ” << b << “, c = ” <<
c << “, d = ” << d;
2. a = 5;
b = 10;
cout << “a= ” << a << “, b= ” << b << endl;
cout <<“++a = ” << a++ << “--b= ” << b-- << endl;
cout << “a= ” << a << “, b= ” << b << endl;
a=5, b=10
a++=5, b--=10
a=6, b=9
Exercise
int x=6, y=20, a, b;
a=20, b=5
a=y++; x=5, y=22
b=--x; x=4, y=22
a+b=8, b=4
cout << “a= ” << a << “, b= ” << b << endl;
cout << “x= ” << x-- << “, y= ” << ++y << endl;
a=--b;
cout << “x= ” << x << “, y= ” << y << endl;
cout << “a+b=” << a+b << “, b= ” << b << endl;
Exercise
Given the following:
Relationship Operator
Equal to ==
Not equal to !=
Greater than >
Greater than or equal to >=
Less than <
Less than or equal to <=
Logic Operator
NOT, negation !
AND &&
OR ||
Logical Operators - Example
if(!(valid_answer))
cout << “invalid”;