You are on page 1of 53

OPERATORS IN

C++
GROUP 2
You are expected to learn
about:
- Arithmetic Operators - Formatting Output
- Unary Operators - I/O Manipulation
- Binary Operators - The input statement cin >>
- Precedence of Arithmetic Operators - Mathematical Library Functions
- Relational and Logical Operators
- Relational Operators and
Simple predicate
- Expressions
- Arithmetic Expressions
An operator is a symbol that
tells the compiler to
perform specific
mathematical or logical
manipulations. There are
three general classes of
operators in C++:
Arithmetic, Relational and
Logical.
ARITHMETIC 2 TYPES OF
OPERATORS ARITHMETIC
The basic arithmetic operators in
C++ are the same as in most other
OPERATORS:
computer languages, and The Unary and Binary operator
correspond to our usual
mathematical/algebraic symbols.
UNARY OPERATORS
OPERATOR FUNCTION
Unary arithmetic operators that
- Unary minus
requires one operand. They include
+ Unary plus unary plus (+), unary minus (-),
and the increment operators ++ and
-- Decrement
decrement operators --.
++ Increment
UNARY PLUS (+) AND
MINUS (-)
UNARY PLUS (+)

The unary plus (+) is used to indicate a positive value,


although it is not necessary because numerical values are
generally assumed to be positive by default. It does not
change the value of the number. .
UNARY MINUS (-)
the unary minus (-) is used to
indicate a negative value. It changes
the sign of the number to its
opposite.
INCREMENT AND
DECREMENT OPERATOR (++)
INCREMENT OPERATOR (+
+)

It increases the value by 1 to the


operand and then stores the value
to the variable.
DECREMENT OPERATOR
(--)

It decreases the value by 1 to the


operand and then stores the value to
the variable.
DECREMENT AND
INCREMENT OPERATOR (--)

However, there is a difference when they are used in an


expression. if the operator occurs before the variable, it pre- increment
is called pre-increment, which means, it will increment ++ (variable)
the variable first before it is stored. On the other hand, if post- increment
the operator comes after the variable, it is called a post- (variable) ++
increment, which means it will store the value first
before it increment
BINARY OPERATORS
OPERATOR FUNCTION

- subtraction binary operators +, -, *, / are


+ addition arithmetic operators in
* multiplication programming used to perform
/ division
operations between two numerical
operands.
% modulus
Subtraction (-)

The subtraction operator is used to


subtract one numerical value from
another. When used, the numerical
value on the right side of the
operator is subtracted from the
value on the left side.
Subtraction (-)
Addition (+)

The addition operator is used to


add two numerical values together.
When used, the numerical values
are combined to obtain their sum.
Addition (+)
Multiplication (*)

The multiplication operator is used


to multiply two numerical values.
When used, the numerical values
are multiplied to obtain their
product.
Multiplication (*)
Division (/)

The division operator is used to


divide one numerical value by
another. When used, the numerical
value on the left side of the
operator is divided by the value on
the right side.
Division (/)
MODULUS OPERATOR
%The fifth operator, %, is called the modulus
operator. It computes the remainder of the
division of its first operand by its second
operand. There are a number of applications
which require a form of division of integers
which allow you to divide one integer by
another and get an integer answer.
MODULUS OPERATOR
%
It is important to note that its operands must
1
be integer values.
Here is an example : 5 % 3 = 2 3 5
3
This expression computes the value of 2
because the division of 5 by 3 yields an 2
integer quotient of 1 with an integer
remainder of 2.
MODULUS OPERATOR
%
(SAMPLE PROBLEM 3.1)
The program below computes for the sum and average of 2 numbers. The arithmetic operators + and
/ are used.
PRECEDENCE OF
ARITHMETIC OPERATORS

C++ compilers are designed to


obey some operator precedence
and associativity rules.
RULES FOR EVALUATION OF
ARITHMETIC EXPRESSIONS
1.First, evaluate all parenthesized sub-expressions, beginning
with the innermost sub-expression in case they are nested.
2. If there are two or more unnested parenthesized sub-
expressions in a sub-expression, use the left associativity rule:
evaluate them left to right.
3. Operators in un-parenthesized sub-expression are applied
using the following precedence rules.
Precedence of arithmetic operators
LATIONAL AND LOGICAL OPERATORS

The relational operators are used to determine the


relationship of one quantity to another. In the
term logical operator the word logical refers to
the ways these relationships can be connected
together using the rules of formal logic.
LOGICAL OPERATORS
Logical operators in C++ are used
to combine multiple conditions in
OPERATOR
SYMBOL decision making statements. That
NAME
includes AND (‘&&’), OR (‘||’),
AND && and NOT (‘!’) , enabling the
programmers to create complex
OR || conditions for more precise control
flow in their code.
NOT !
TRUTH TABLE
A&&B !A
A B A||B (OR)
(AND) (NOT)

TRU
TRUE TRUE TRUE TRUE
E

FALS
TRUE FALSE TRUE FALSE
E

FALS TRU
FALSE TRUE TRUE
E E
RELATIONAL
OPERATORS
Operator Name Symbol

Less than <

Less than or equal to <=

Greater than >

Greater than or equal to >=

Equal ==

Not equal to !=
RELATIONAL OPERATORS
Example: Result:
RELATIONAL and LOGICAL OPERATORS
Example:
Truth Table
RELATIONAL
OPERATORS AND
SIMPLE PREDICATES
Conditional expressions are formed using relational and
logical operators. An expression that compares two
expressions using relational operator is called simple predicate.
A simple predicate computes to either 1 (for true) or o (for
false). There are six relational operators in C.
PRECENDENCE OF ARITHMETIC AND
RELATIONAL OPERATORS
SAMPLE PROBLEM 3-
3
SAMPLE PROBLEM 3-
4
EXPRESSION
In arithmetic there are two expressions:
S
1.Integral Expression EXAMPLE
- An integer expression is an arithmetic
100+2+6
expression in which all operands are
integer. 200-10*60+2

2. Floating Expression
- A float expression is an arithmetic
75.5+80.75-30.25*75.85
expression in which all operands are
floating point.
80.50-10.25*60.0+95.26
MIXED EXPRESSIONS
A mixed expression involves operands of different data types, combining both
integers and floating-point numbers within the same mathematical expression.

Rules for Evaluating Mixed Expressions:


1.Operand Types:
- If both operands are of the same type (either both integers or both floating-
point numbers), the operator is evaluated according to the type of the
operands.
MIXED
- If the operator has mixed types of operands (one integer and one floating-point
EXPRESSIONS
number), the integer is converted to a floating-point number during calculation.

2.Precedence Rules:

- Follow the order of precedence; multiplication, division, and modulus operators


are evaluated before addition and subtraction.
- Operators with the same level of precedence are evaluated from left to right.

- Grouping can be used for clarity.


MIXED
EXPRESSIONS
ARITHMETIC EXPRESSIONS

The combination of operands and operators. It is a sequence of variables and


constants separated by arithmetic operators. The following are examples of a
simple expression:

int sum = a + b;
int difference = a- b;
int product = a * b;
float quotient = static_cast<float>(a) / (b)
int modulus remainder = a % b;
FORMATTING OUTPUT
The cout operator guesses how you want your data printed. Sometimes, Visual
C++ makes an incorrect as to how cout should produce a value, but most of the
time cout suffices for any output you require, as shown below:

//cout can output integer


cout<< 70; //print a 70

//cout can output floating point values;

cout<<3-1416 //print 3.1416


I/O
In Visual C++,
MANIPULATORproper formatting of output is essential, especially when dealing
with numeric values. I/O manipulators are tools that allow control over how data
is displayed.
1.Precision() Manipulator:
The precision() manipulator is employed to control the number of digits
displayed after the decimal point in numeric output. This is crucial when precise
representation is needed. For instance, using cout.precision(4) sets the precision
to four decimal places.
Examples:
- Without precision: cout << 4.5000 outputs 4.5.
- Using precision: cout.precision(4); cout << 4.500 outputs 4.5000.
I/O
MANIPULATOR
2.Width() Manipulator:
The Width() Manipulatois utilized to control the width of the field in which
output is displayed. It allows for alignment and spacing adjustments in the
output.
Sample Problem 3-6: Using width():
The program demonstrates the use of width() to format a calendar output,
creating structured and aligned columns for days and dates
The Input Statement cin>>

The cin (pronounced as “see-in”) object provides for input from, the
terminal(keyboard). It stands for console input.

The declaration of the library function cin >> is contained in the


header file iostream.h Therefore, for interactive input, you must have
the preprocessor directive #include <iostream.h> in your source file:
MATHEMATICAL LIBRARY
FUNCTIONS
The basic arithmetic operations recognized by the compiler appear to
be limited to unary plus, unary minus, increment, decrement and the
four basic mathematical operations.

What if we want to compute square root, exponentiations,


logarithms, and so on? For instance, how is the distance formula or
the Pythagorean theorem expressed in C++ language?
MATHEMATICAL LIBRARY
FUNCTIONS
The distance formula below will be expressed using mathematical library
functions that come together with your C++ compilers.

Using math functions the distance formula would be written in this way:

Mathematical library functions must be declared in the header files just like the
input-output library functions.
MATHEMATICAL LIBRARY
FUNCTIONS
The appropriate preprocessor directives used for floating point type function
declarations are found in the math.h header file. Hence the directive used is:

#include<math.h>

On the other hand, the integer type mathematical functions require the
preprocessor directive:

#include<stdlib.h>
MATHEMATICAL LIBRARY
FUNCTIONS
Shown below is a table of some important mathematical library functions and
their uses.
MATHEMATICAL LIBRARY FUNCTIONS (SAMPLE
PROBLEM 3.8)
THAN
K

You might also like