Documente Academic
Documente Profesional
Documente Cultură
OPERATORS
Assignment
Operator
Mathematical
Operators
Relational Operators
Logical Operators
INTRODUCTION
Operators are symbols used to represent computer
operations.
These symbols have one or more special characters
defined by programming language.
The operations that can be operated on variables are:
Arithmetic operation.
ASSIGNMENT OPERATOR
In certain languages, for example in C language,
equal (=) symbol is used as symbol in assignment
operations.
This operator is used to change variables value.
Example:
x=5
5 are assigned to variable x.
Example:
x=y=0
The result from the operations: variable x and
variable y are assigned to the same value, which is 0.
One type of assignment operator is the compound
assignment operator.
Maksud / Meaning
a += b
a=a+b
a *= 5
a=a*5
a = 2
a=a2
a %= 3
a=a%3
Example:
If a = 2, b = 4
a += b (means a = a + b)
Result: a = 6
If b = 5
b -= 2 (means b = b 2)
Result: b = 3
CONT
If a = 2, b = 5
a += b (means a = a
+ b)
b = a (means b + b a)
Result: a = 6
Before:
a 2
100
After:
a
6
10
0
Result: b = 3
Before:
4
20
0
a
After:
b 4
20
0
2
100
2
100
5
200
b 3
200
ARITHMETIC OPERATOR
Arithmetic operation is the calculation operation that can be
done on data in variables.
In C language, the 5 arithmetic operators are:
Symbol
+
Operators
Add
Subtract
Multiply
Divide
Modulus
CONT
Increment and Decrement Operation
Variables usually can be increased or decreased by 1.
For example, C language and Java has provided
operator to add or subtract 1 from variable values.
The symbols are:
Symbol
Operation
++
Add 1
Subtract 1
Example:
x++ can also be written as x = x + 1
y can also be written as y = y 1
RELATIONAL OPERATOR
Relational operator can be divided into 2 groups:
1.Not Equivalent Group
Not
Greater than
>=
<
to
<=
Less than
Less than or equals to
Example:
a = 10, b = 2;
c = a > b;
d = (b * 3) > a;
Variable
Variable cc will
will have
have 11 as
as
value
value because
because relation
relation
(10
>
2)
is
true,
(10 > 2) is true, while
while
variable
d
will
have
variable d will have 00
because
because relation
relation (6
(6 << 0)
0)
isisfalse.
false.
CONT
2. Equivalent Group
Equivalent
Example:
a = 10;
b = 2;
c = a != b;
d = (b * 5) == a;
Symbol
Operators
==
Equal to
!=
Not equal to
Variable
Variableccwill
willhave
have11as
asvalue
value
because
relation
(10!
=
2)
because relation (10! = 2)isistrue,
true,
and
variable
d
will
have
1
as
value
and variable d will have 1 as value
because
becauserelation
relation(2(2* *55==
==10)
10)isis
also
alsotrue.
true.
LOGICAL OPERATOR
Operators
AND
||
OR
NOT
P&&Q P||Q
!P
!Q
NOT 0
NOT 0
NOT 0
NOT 0
Example 1:
If x = 2 and y = 3
Ungkapan / Expression
a = x && y
Nilai a / Value a
1
a = (x < y) && (y == 0)
a = x || y
a = (x !0) || (y != 0)
a = (x == y) || (y == 0)
a = ! (x == y)
a = ! (x < y)
OPERATOR COMPOUND
Operations
Highest priority
()
!
*
+
<
==
&&
||
=
++
/
<=
!=
%
>
>=
Lowest priority
CONT
Example 1:
Expression x + y == z && m n will be defined
as below:
( x + y ) == z && m n
( x + y ) == z && ( m n )
( ( x + y ) == z ) && ( m n )
( ( ( x + y ) == z ) && ( m n ) )
++operation
operationgets
getshighest
highestpriority
priorityin
inthe
the
expression.
Then,
followed
by
operation
expression. Then, followed by operation
and
andthen
then==
==operation.
operation.The
Thelast
lastoperation
operation
done
doneisis&&
&&because
becauseitithas
hasthe
thelowest
lowest
priority.
priority.
CAST OPERATOR
Cast operator is an operator used to change the types of
expression result.
For example, to change integer type to float type.
Example in C Language:
int i = 9;
float f;
f = i / 2;
From example, variable f will get 4.0. This happened because
variable i is integer type, so dividing the integer number will
cause the decimal part to be cut out of the answer.
To get an accurate answer, the syntax must be changed to:
int i = 9;
float f;
f = (float) i / 2;
Therefore, the result 4.5 will be assigned to variable f.