0% found this document useful (0 votes)
129 views

Operators & Mathematical Expression

The document discusses mathematical expressions and operators in C++. It defines expressions as combinations of constants and variables using operators. It describes arithmetic, relational, and logical operators and their use in expressions. Examples are provided of mathematical expressions written in algebraic and C++ notation. Predefined mathematical functions like sqrt and pow are also summarized.

Uploaded by

Forkenstein
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
129 views

Operators & Mathematical Expression

The document discusses mathematical expressions and operators in C++. It defines expressions as combinations of constants and variables using operators. It describes arithmetic, relational, and logical operators and their use in expressions. Examples are provided of mathematical expressions written in algebraic and C++ notation. Predefined mathematical functions like sqrt and pow are also summarized.

Uploaded by

Forkenstein
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 27

Operators and

Mathematical
Expressions

Mathematical Expressions

An expression can be a constant, a


variable, or a combination of constants
and variables combined with operators.
Can create complex expressions using
multiple mathematical operators.
Example mathematical expressions
2
height
a + b / c

Using Mathematical Expressions

Can be used in assignment statements,


with cout, and in other types of
statements

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

These are the symbols that tells the


compiler to perform a specific
mathematical or logical manipulation.

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

- (unary negation) associates right to left


* / % + - all associate left to right
parentheses ( ) can be used to override the
order of operations
2 + 2 * 2 2 = 4
(2 + 2) * 2 2 = 6
2 + 2 * (2 2) = 2
(2 + 2) * (2 2) = 0

Example: Evaluate the


following expressions
a = (5 + 4) * 3
b=5+4*3
c = 50 / 10 * 2
d = 60 - 20 + 4 / 2 * 10

Division (/) Operator

C++ division operator (/) performs integer


division if both operands are integers
cout << 13 / 5;
cout << 2 / 4;

// displays 2
// displays 0

If either operand is floating-point, the


result is floating-point
cout << 13 / 5.0; // displays 2.6
cout << 2.0 / 4; // displays 0.5

Modulus (%) Operator

C++ modulus operator (%) computes the


remainder resulting from integer division
cout << 9 % 2;

// displays 1

% requires integers for both operands


cout << 9 % 2.0; // error

Algebraic Expressions

Multiplication requires an operator


Area = lw is written as Area = l * w;
There is no exponentiation operator
Area = s2 is written as Area = pow(s,2);
Parentheses may be needed to maintain
order of operations
y 2 y1
m
x 2 x1

is written as
m = (y2-y1)/(x2-x1);

Mathematical Algebraic Mathematical Algebraic


Expression
Expression in C++

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)

Some Predefined Functions in


C++

Predefined functions - are functions that


are built into C++ to perform some
standard operations.
These functions were predefined for you,
meaning that you didn't have to tell the
computer how to compute the square
root and power of a number.

Some Predefined Functions in


C++

Name

The header file required for


mathematical functions is #include
<cmath>
Descriptio Data
Type of
Example
Value
Library
n

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

Used to compare numbers to


determine relative order
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 are Boolean


(i.e., evaluate to true or false)
Examples:
12 > 5 is true
7 <= 5 is false
if x is 10, then
x == 10
x != 8
x == 8

Relational Expressions

Assigns 0 for false, 1 for true

Do not confuse = and ==

Logical Operators
Used to create relational expressions
from other relational expressions.
Sometimes referred to as Boolean
operators.
Logical
Operators:

&& AND

New relational expression is true if both


expressions are true

|| OR

New relational expression is true if either


expression is true

NOT

Reverses the value of an expression;


true expression becomes false, and
false becomes true

Truth Table for the && (logical AND)


operator
Value of
Value of
Value of
Expression Expression Expression
1
2
1 &&
Expression
2
True
True
False
False

True
False
True
False

True
False
False
False

Truth Table for the || (logical OR)


Operator
Value of
Value of
Value of
Expression Expression Expression
1
2
1 ||
Expression
2
True
True
False
False

True
False
True
False

True
True
True
False

Logical Operator Examples


int x = 12, y = 5, z = -4;

(x > y) && (y > z)

true

(x > y) && (z > y)

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

You might also like