Operators & Mathematical Expression
Operators & Mathematical Expression
Mathematical
Expressions
Mathematical Expressions
Examples:
area = 2 * PI * radius;
cout << "border is: " << (2*(l+w));
Assignment Statement
Uses the equal sign = operator
Has a single variable on the left side and
a value on the right side
Copies the value on the right into the
variable on the left
x = 5;
y = 6;
z = 4;
x = x + y;
y = y + 4;
z = 5 + x + z;
Operators
Types of Operators:
Arithmetic
2. Relational
3. Logical
1.
Arithmetic Operators
SYMBOL
OPERATION
EXAMPLE
ans
addition
ans = 7 + 3;
10
subtraction
ans = 7 - 3;
multiplication
ans = 7 * 3;
21
division
ans = 7 / 3;
modulus
ans = 7 % 3;
Precedence of Operators
Associativity of Operators
// displays 2
// displays 0
// displays 1
Algebraic Expressions
is written as
m = (y2-y1)/(x2-x1);
abc
a*b*c
a + bc
a+b*c
y3
pow (y , 3) or y * y * y
a
------------b+c
a / (b+c)
a(a+b)
a * (a + b)
Name
Types
Value
Returned
Header
sqrt
Square
Root
double
double
sqrt(4.0)
2.0
cmath
pow
Powers
double
double
pow(2.0,3
.0)
8.0
cmath
Relational Operators
Greater than
Less than
Greater than or equal to
Less than or equal to
Equal to
Not equal to
Relational Expressions
Relational Expressions
Logical Operators
Used to create relational expressions
from other relational expressions.
Sometimes referred to as Boolean
operators.
Logical
Operators:
&& AND
|| OR
NOT
True
False
True
False
True
False
False
False
True
False
True
False
True
True
True
False
true
false
(x <= z) || (y == z)
false
(x <= z) || (y != z)
true
!(x >= z)
false
Logical Precedence
Highest
Lowest
&&
||
Example:
(2 < 3) || (5 > 6) && (7 > 8)
is true because AND is done before OR
More on Precedence
Highest
Arithmetic Operators
Lowest
Relational Operators
Logical Operators
Example:
8 < 2 + 7 || 5 == 6
is true
Knowledge Check
Evaluate the following logical expressions:
a=15, b=20, c=-5 d=10
1. b >= d && c!=-5 || c >= 100
2. a == 15 && 120 < 0
3. ! (30 != b) || c>=-5 && 6+5 > d
That in All
Things,God May Be
Glorified!
Thank you!
Prepared by: Prof. LRQ Natividad