You are on page 1of 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

cout << "border is: " << (2*(l+w)). . and in other types of statements  Examples: area = 2 * PI * radius.Using Mathematical Expressions  Can be used in assignment statements. with cout.

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. z = 4. y = 6.  . z = 5 + x + z. x = x + y. y = y + 4.

Types of Operators: Arithmetic 2.Operators  These are the symbols that tells the compiler to perform a specific mathematical or logical manipulation. Logical 1. . Relational 3.

10 - subtraction ans = 7 . 21 / division ans = 7 / 3. 1 .Arithmetic Operators SYMBOL OPERATION EXAMPLE ans + addition ans = 7 + 3. 4 * multiplication ans = 7 * 3.3. 2 % modulus ans = 7 % 3.

Precedence of Operators .

(unary negation) associates right to left * / % + .Associativity of Operators    .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 .

20 + 4 / 2 * 10 .Example: Evaluate the following expressions a = (5 + 4) * 3 b=5+4*3 c = 50 / 10 * 2 d = 60 .

6 cout << 2. cout << 2 / 4. the result is floating-point cout << 13 / 5.5 .0.  // displays 2 // displays 0 If either operand is floating-point.0 / 4. // displays 2.Division (/) Operator  C++ division operator (/) performs integer division if both operands are integers cout << 13 / 5. // displays 0.

// error .  // displays 1 % requires integers for both operands cout << 9 % 2.Modulus (%) Operator  C++ modulus operator (%) computes the remainder resulting from integer division cout << 9 % 2.0.

. Parentheses may be needed to maintain order of operations y 2  y1 m x 2  x1 is written as m = (y2-y1)/(x2-x1).2). There is no exponentiation operator Area = s2 is written as Area = pow(s.Algebraic Expressions    Multiplication requires an operator Area = lw is written as Area = l * w.

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 cmath pow Powers double double pow(2.3 .0) 2.0.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 .

then x == 10 x != 8 x == 8 ..e.Relational Expressions   Relational expressions are Boolean (i. evaluate to true or false) Examples: 12 > 5 is true 7 <= 5 is false if x is 10.

Relational Expressions  Assigns 0 for false. 1 for true  Do not confuse = and == .

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.Logical Operators Used to create relational expressions from other relational expressions. and false becomes true . true expression becomes false. Sometimes referred to as Boolean operators.

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 .

y = 5. (x > y) && (y > z) true (x > y) && (z > y) false (x <= z) || (y == z) false (x <= z) || (y != z) true !(x >= z) false .Logical Operator Examples int x = 12. z = -4.

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 .

b >= d && c!=-5 || c >= 100 2. a == 15 && 120 < 0 3. ! (30 != b) || c>=-5 && 6+5 > d . b=20.Knowledge Check Evaluate the following logical expressions: a=15. c=-5 d=10 1.

That in All Things. God May Be Glorified! .

LRQ Natividad .Thank you!  Prepared by: Prof.