L6 - OPERATORS, EXPRESSIONS AND ASSIGNMENTS
L6 - OPERATORS, EXPRESSIONS AND ASSIGNMENTS
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.
…