CSC201 Lecture 2 Slide
CSC201 Lecture 2 Slide
2
Arithmetic Operators
3
Arithmetic Operators
However, some of these operators can be combined to produce
a different operation, eg ++ and ---
4
Arithmetic Operators
Let’s consider
the following
example:
5
Relational Operators
Operator Description
Checks if the values of two operands are equal or not, if yes then
== the condition becomes true.
Checks if the values of two operands are equal or not, if values are
!= not equal then the condition becomes true.
Checks if the value of the left operand is greater than the value of
> the the right operand, if yes then the condition becomes true.
Checks if the value of the left operand is less than the value of the
< right operand, if yes then the condition becomes true.
Checks if the value of the left operand is greater than or equal to
the value of the right operand, if yes then the condition becomes
>= true.
Checks if the value of the left operand is less than or equal to the
<= value of the right operand, if yes then the condition becomes true.
6
Relational Operators
Let’s consider the following example:
7
Logical Operators
8
Logical Operators
Let’s consider the following example:
9
Bitwise Operators
Operator Description
Binary AND Operator copies a bit to the result if it exists in both
& operands.
Binary Left Shift Operator. The left operands value is moved left
<< by the number of bits specified by the right operand.
Binary Right Shift Operator. The left operands value is moved
>> right by the number of bits specified by the right operand.
10
Bitwise Operators
Now let’s assume a=20 and b= 14 and they are both stored in an 8 bit register
a=0001 0100 b=0000 1110
To perform the bitwise operation you compare the bit position of the operands
Operation Outcome Decimal Value
0001 0100
(A & B) 0000 1110 4
0000 0100
0001 0100
(A | B) 0000 1110 30
0001 1110
0001 0100
(A ^ B) 0000 1110 26
0001 1010
0001 0100
(~A ) -21
1110 1011
0001 0100
A << 2 80
0101 0000
0001 0100
A >> 2 3
0000 0011
11
Bitwise Operators
12
Assignment Operator
The assignment operator can be combined with other operators. The table below
shows the various combinations.
13
Assignment Operator
14
Assignment Operator
15
Assignment Operator
Let’s consider the following
example
16
Misc Operators
C++ supports other operators and the table below shows other operators that C++ supports.
Operator Description
Cast Casting operators convert one data type to another.
Comma operator causes a sequence of operations to be performed. The
value of the entire comma expression is the value of the last expression of
, the comma-separated list.
Conditional operator (?). If Condition is true then it returns value of X
Condition ? X : Y otherwise returns value of Y.
& Pointer operator & returns the address of a variable.
* Pointer operator * is pointer to a variable.
sizeof operator returns the size of a variable. For example, sizeof(a),
Sizeof where ‘a’ is integer, and will return 4.
17
Misc Operators
Cast
A cast is a special
operator that
forces one data
type to be
converted into
another data
type.
18
Misc Operators
, (Comma)
The comma
operator (,) is used
to separate two or
more expressions
that are included
where only one
expression is
expected.
19
Misc Operators
Conditional ? : Operator
The conditional
operator evaluates an
expression returning a
value if that expression
is true and a different
one if the expression is
evaluated as false.
Its format is:
20
Misc Operators
Sizeof Operator
It is a compile-time
operator that
determines the
size, in bytes, of a
variable or data
type
21
Misc Operators
& and * (Pointer Operator)
22
Precedence of Operator
Operator precedence determines how an operator is evaluated in an expression
when compared to other operator.
23
Precedence of Operator
Level Precedence Operator Description Grouping
group
2 Prefix (unary) ++ -- prefix increment / Right-to-left
decrement
~! bitwise NOT / logical
NOT
+- unary prefix
&* reference / dereference
24
Precedence of Operator
Level Precedence group Operato Description Grouping
r
4 Pointer-to-member .* ->* access pointer Left-to-right
26
Basic Input/Output
The standard C++ library includes the header file iostream, where the
standard input and output stream objects are declared.
cout is used in conjunction with the insertion operator, which is written as <<
Standard Input (cin): The standard input device is usually the keyboard.
Handling the standard input in C++ is done by applying the overloaded
operator of extraction (>>) on the cin stream.
The operator must be followed by the variable that will store the data that is
going to be extracted from the stream.
27
Basic Input/Output
28