0% found this document useful (0 votes)
15 views18 pages

L6 - OPERATORS, EXPRESSIONS AND ASSIGNMENTS

Uploaded by

lkixome
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)
15 views18 pages

L6 - OPERATORS, EXPRESSIONS AND ASSIGNMENTS

Uploaded by

lkixome
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/ 18

OPERATORS,

EXPRESSIONS AND
ASSIGNMENTS
By Sir. Joshua
Operators
 An operator is a symbol that tells the
compiler to perform specific mathematical or
logical manipulations
 Operators are used to perform operations on
variables and values.
 In the example below, we use the + operator
to add together two values:
Types of operators
C++ is rich in built-in operators and provides
the following types of operators:
 Arithmetic Operators
 Relational Operators
 Logical Operators
 Bitwise Operators (eg: AND (&), OR (|),
NOT (~), XOR (^), etc)
 Assignment Operators
Arithmetic Operators:
 Arithmetic
operators are used to perform
common mathematical operations.
 There
following arithmetic operators are
supported by C++ language:
 Assume variable x holds 10 and variable y
holds 20, then:

Operat Name Description Example
or
+ Addition Adds together two values x + y (=
30)
- Subtraction Subtracts one value from x – y (= -
another 10)
* Multiplication Multiplies two values x * y (=
200)
/ Division Divides one value by another x / y (=
0.5)
% Modulus Returns the division remainder x % y (=
10)
++ Increment Increases the value of a variable ++x (=
by 1 11)
Relational (Comparison)
Operators
 Relational operators are used to compare
two values (or variables).
 This
is important in programming, because it
helps us to find answers and make decisions.
 The return value of a comparison is either 1
or 0, which means true (1) or false (0).
 These values are known as Boolean values

Assume variable x holds 10 and variable y holds
20, then:
Operator Name Example
== Equal to x == y (=
false)
!= Not equal x != y (=
true)
> Greater than x>y (=
false)
< Less than x<y (=
true)
>= Greater than or equal to x >= y (=
false)
Assignment Operators
 Assignment operators are used to assign values to
variables.
 The syntax used to assign value to the variable is
variable = value;
for example x=10; or y=a+1.
 The assignment operations always take the value
from right to left.
 This means that, if two variables a and b are assigned
as
a = b, then, the value of a will be replaced by the
value of b and not otherwise.

Operator Example Same As


= x=5 x=5
+= x += 3 x=x+3
-= x -= 3 x=x-3
*= x *= 3 x=x*3
/= x /= 3 x=x/3
%= x %= 3 x=x%3
Logical Operators
 Aswith comparison operators, you can also
test for true (1) or false (0) values with
logical operators.
 Logicaloperators are used to determine the
logic between variables or values:

Assume variable x holds 10 then:

Opera Name Description Example


tor
&& Logical Returns true if both x < 5 && x <
and statements are true 10
(= false)
|| Logical Returns true if one of the x < 5 || x < 4
or statements is true (= false)
! Logical Reverse the result, returns !(x < 5 && x <
not false if the result is true 10)
(= true)
Increment and decrement
operators
 Increment (++) and decrement (--) operators are
unary operators that can add or subtract one to
the variable.
 Theincrement operators add one to the variable
while decrements subtract one. The increments
and decrements operators can be written before
variable as prefixes and after a variable as the
suffixes.
 Theincrement and decrement operators are
mainly used to iterate integers and floating data
types

Mathematical expressions in
C++ programming
 Like
other languages, the C++ programming
language provides rules through which different
data, including variables, constants and
operators, can be combined.
 Suchan interconnection of data is known as
expression.
 Duringprogramming, the arithmetic expression
should be translated into C++ expression so that
a compiler can understand what is programmed.
Examples of expressions
 Mathematical expression:
C++ Equivalent expression: (1/2)
 Mathematical expression:
C++ Equivalent expression:
 Mathematical expression:
C++ Equivalent expression: (
Operator precedence in C++
 Levels of precedence between operators, both in
arithmetic expression and C++ expressions, remain
the same.
 That level starts from the operation enclosed by
brackets, followed by arithmetic operators out of the
brackets.
 Priority is given to division and multiplication, followed
by addition and subtraction.
 If the expression includes several arithmetic operators
of the same type the operation will be conducted from
left to right.
Activity
 Writea C++ program that can enable a user
to convert Fahrenheit to Centigrade degree.
Use the formula,
 Design an algorithm and then write a C++
program that will enable customers to know
the Value-Added Tax to be paid for any
purchase price of the commodity. The VAT
rate of 18.2% should be declared as a
constant, and the formula should be

 Design an algorithm and then create a C+
+ program that prompts a user to enter a
radius of the sphere. The program should
calculate and display the volume of the
sphere as output. (Hint: The formula for
calculating volume of sphere is , r
represents radius of the sphere. Use Table
below in your declaration.

You might also like