0% found this document useful (0 votes)
10 views39 pages

Chap04 - Operators Expressions

Uploaded by

black hello
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views39 pages

Chap04 - Operators Expressions

Uploaded by

black hello
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 39

C++ Programming:

Problem Solving and


Programming

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

• A sequence of operands and operators that


reduces to a single value.
• No limit to the number of operators and operands
in an expression.
• An operator is a syntactical token that requires an
action to be taken. ( +, -, *, /, % …).
• An operand receives an operator’s action.
Expressions
Operator

• E.g. expression: 3 + 6 - 4

Operand

• Can be simple (contains only one operator) or


complex (contains more that one operator).
• E.g. simple expression 🡪 2 + 5
• E.g. complex expression 🡪 7 + 12 * 3
Types of Operators

• 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;

Example 2 (Use of cast operator):


int num1=12, num2=5;
double total;
total =num1/num2; total
=(double)num1/num2;
Assignment Operators
• Evaluates the operand on the right side of the
operator (=) and places its value in the variable
on the left.

• The left operand must be a single variable.


a = b + 10;
temperature = 98.6;
count = count + 2;
Assignment Operators
• Simple assignment – found in algebraic
expressions.
x = 9;
y = x + 10;
z = y * x;
• Multiple assignment
a = b = c = d = e = 100;
− The assignment associates from right to left
• Assignment within another calculation.
value = 5 + (s = 5 - a)
Assignment Operators
• Compound assignment - A shorthand notation for a
simple assignment.
• Requires the left operand be repeated as a part of the
right expression.
• Five compound assignment operators:
*= %= -=
/= +=
• E.g.
x *= y + 5 evaluated as x = x *( y + 5 )
Compound Assignment
Compound Assignment Equivalent Simple Assignment
x += y x=x+y

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

• Precedence is used to determine the order in


which different operators in a complex
expression are evaluated.

• Associativity is used to determine the order in


which operators with the same precedence are
evaluated in a complex expression.
Order of Precedence

• All operations inside of parenthesis ( ) are


evaluated first
• The higher precedence operators in the table will
be performed first as compared to the lower ones
(refer the table on next slide)
• When operators are on the same level
⮚ Perform from left to right
Order of Precedence
Order of Precedence - Example

• 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

■If a=3, b=5, c=8;


a+=b*=c-=5;

■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 :

(++a) and (a++) has the same effect as (a = a + 1)


Prefix & Postfix Expression

• 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

FIGURE 4-6 Result of Prefix ++a


Prefix Expression - Examples

1. int a, b = 5, c, d = 10;
c = ++d;
a = --b;
cout << “a= ” << a << “, b= ” << b << “, c = ” <<
c << “, d = ” << d;

a=4, b=4, c=11, d=11


Prefix Expression - Examples

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

FIGURE 4-8 Result of Postfix a++


Postfix Expression - Examples

1. int a, b = 5, c, d = 10;
c = d++;
a = b --;
cout << “a= ” << a << “, b= ” << b << “, c = ” <<
c << “, d = ” << d;

a=5, b=4, c=10, d=11


Postfix Expression - Examples

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:

int x=9, y=5, z=31, m=1234;

Find the value of the following expressions. Assume


all the
questions are independent (not related).
a) 14
a) x++ + y++
b) ++x - --z b) -20
c) z – (x + z) % 2 + 4 c) 35
d) x – 4 * (3 + z) + y
e) (m/10) % 10
d) -122
e) 3
Relational Operators
• Always produces a TRUE(nonzero) or
FALSE (0) result.
• E.g.
int a = 5, b =10, c = 15, d = 5;
− a>b
( return 0)
− b >= a b<c d <= b c>a
( all return nonzero value )
Relational Operators

Relationship Operator
Equal to ==
Not equal to !=
Greater than >
Greater than or equal to >=
Less than <
Less than or equal to <=

e.g. if(mark > 100)


cout << “invalid”;
Logical Operators
• Logical data have only two values: TRUE or FALSE
• If a data value is zero, it is considered false. If it is
nonzero, it is considered true.
Zero ≡ FALSE
Nonzero (positive / negative) ≡ TRUE
Logical Operator

Logic Operator
NOT, negation !
AND &&
OR ||
Logical Operators - Example

if(!(valid_answer))
cout << “invalid”;

if(mark < 0 || mark >


100)
cout << “invalid”;
x = 1;
while(x <= 10) {
cout << x << endl;
x++;
}
Exercise
Given int i = 7, float f = 5.5 and char c
= ‘w’
Expression Meaning
a) f > 5 True
b) (i + f) <= 10 Fals
c) c == 119 Truee
d) c != ‘p’ True
e) c >= 10 * (i +f) Fals
f) (f < 11) && (i > 100) e
Fals
g) (i >= 6) || (c == 119) e
True
h) !(f > 5) Fals
e
Ternary Conditional Operators

• An alternative to the if…else for two-way selection.


• Format:
expression1 ? expression_A :
expression_B

• Evaluate the leftmost expression (expression1)


first.
• If expression1 is TRUE, the value of the
conditional expression is the value of
expression_A. Otherwise, the value will be the
value of expression_B.
Ternary Conditional Operators
• If a equal to b, c-- will be evaluated and c++
will be ignored.
• If a not equal to b, c++ will be evaluated and
c-- will be ignored.

You might also like