0% found this document useful (0 votes)
10 views25 pages

B) Introduction To C++

The document provides an overview of various types of operators in programming, including unary, binary, logical, relational, arithmetic, bitwise, and ternary operators. It explains the function and examples of each operator type, detailing how they manipulate operands in expressions. Additionally, it includes a section on special operators and a series of questions to test understanding of the material.

Uploaded by

tan
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)
10 views25 pages

B) Introduction To C++

The document provides an overview of various types of operators in programming, including unary, binary, logical, relational, arithmetic, bitwise, and ternary operators. It explains the function and examples of each operator type, detailing how they manipulate operands in expressions. Additionally, it includes a section on special operators and a series of questions to test understanding of the material.

Uploaded by

tan
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/ 25

OPERATORS

Operators are symbols


that tells the compiler to
perform specific mathematical
or logical manipulations.
These operators can
be either unary, binary, ternary
or special operators. Operands
are constants or variables on
UNARY OPERATORS
Unary operators are those operators
that act on one operand. The different unary
operators are:
Logical NOT (!)
Use to reverse the logical state of its
operand. If a condition is TRUE then the result
of logical NOT is FALSE.
Address-of(&)
Use to give the address of the operand.
One’s complement(~)
Converts 1’s to 0 and 0’s to 1.
Pointer dereference(*)
Unary (+)
Used to represent a signed positive
operand.
Unary negation (-)
Used to represent a signed
negative operand.
Increment (++)
Used to increment an operand by 1.
Decrement (--)
Used to decrement an operand by
1.
++ is used to increment the
value of the variable by 1. The
operator can appear before or after
the variable. If ++ is used to the left
of the variable, then it is the ‘pre-
increment’ operator. If it is used to
the right of the variable then
a=10; it is the
a=10;
b=a++; b=++a;
‘post- increment’ operator.
Therefore, a=11;
Therefore, a=11;
Example: b=10; b=11;
-- is used to decrement
the value of the variable by one.
The operator can appear before
or after the variable. If it is
used to the left of the variable,
then it is ‘pre- decrement’
a=10; a=10;
operator. If it is used to the b=--a;
b=a--;
right ofTherefore,
the variable,
a=9; then it
Therefore, is
a=9;
b=9;
‘post- decrement’ b=10;operator.
Example:
BINARY OPERATORS
The binary operators
are those operators that
operate on two operands.
They are arithmetic,
relational, logical, bitwise
and assignment
operators.
ARITHMETIC OPERATORS
The different
arithmetic operators are:
● Addition (+)
● Subtraction (-)
● Multiplication (*)
● Division (/)
● Modulus (%)
Let a= 10 and b=5

Operator Description Example


+ Adds two operands a+b
gives 15
- Subtracts the second a-b gives
operand from the first 5
* Multiplies both a*b gives
operands 50
/ Divides numerator by a/b gives
denominator 2
% Gives the remainder a%b
RELATIONAL OPERATORS
A relational operator compares two
data values and determines the relation
between them. A relational operator generates
an output of either true or false. The relational
operators supported by C++ are:
== Equal to
!= Not equal to
> Greater than
< Lesser than
>= Greater than or equal to
Let a = 5 and b = 10

OPERATOR DESCRIPTION EXAMPLE


== Checks if the value of two a==b
operands is equal gives
false
!= Checks if the value two a!=b
operands is not equal gives
true
> Checks if the value of the left a>b
operand is greater than the gives
value of the right operand and false
returns true if the condition is
true otherwise false
< Checks if the value of the left a<b
LOGICAL OPERATORS

Logical operators are


normally used when our action
depends on the output of multiple
conditions.
The logical operators that
are used in C++ are
● Logical AND (&&)
● Logical OR (||)
LOGICAL AND (&&)
It is used to combine two relational
expression. It returns true if both the
expressions are true. OtherwiseRESULT
OPERANDS it returns
false. &&
OPERAND 1 OPERAND 2

TRUE TRUE TRUE

TRUE FALSE FALSE

FALSE TRUE FALSE

FALSE FALSE FALSE


LOGICAL OR (||)
It is used to combine two relational
expression. It returns true if either of the
expressions are true. Otherwise it
OPERANDS returns
RESULT
false. ||
OPERAND 1 OPERAND 2

TRUE TRUE TRUE


TRUE FALSE TRUE
FALSE TRUE TRUE
FALSE FALSE FALSE
LOGICAL NOT (!)
It is used to negate the operation.
Returns true if the expression is false and
returns false if the expression is true.
OPERAND RESULT
!

TRUE FALSE

FALSE TRUE
ASSIGNMENT OPERATORS
It is used to assign either a
value or an expression to a variable.
Syntax :
variable= value or expression;
Example :
n=10; assigns or stores the
value 10 in the variable ‘n’.
Sum = a +b;
SHORT-HAND OPERATORS
These operators are used to reduce the length of the operation
and represent it in a shorter form in a program. It is also used as an
assignment operator. Few shorthand operators are
▀ +=
C+=5 is equivalent to C=C+5
▀ -=
C-=5 is equivalent to C=C-5
▀ *=
C*=5 is equivalent to C=C*5
▀ /=
C/=5 is equivalent to C=C/5
▀ %=
C%=5 is equivalent to C=C%5
BITWISE OPERATORS
These allows us to perform various
operations on the individual bits of a
memory location. The bitwise operators
are:
Bitwise AND (&)
Bitwise OR (|)
Bitwise XOR (^)
Bitwise NOT (one’s complement) (~)
Bitwise left shift (<<)
Let a = 00111 and b = 01010
Operator Description Example

It gives a ‘1’ (one) if the corresponding bits in a&b gives 00010


& both the values are 1, otherwise it gives a ‘0’
(zero).

The result of an inclusive OR operator is a 1 if a | b gives 01111


| either or both of the corresponding bits is a 1.

It is also called the exclusive OR operator. It a ^ b gives 01101


^ gives a 1 if the corresponding bits in one of the
values are 1, otherwise it gives 0.
a b a&b a|b a^b

0 0 0 0 0
0 1 0 1 1
1 0 0 1 1
1 1 1 1 0
Operator Description Example

It reverses the values of all the bits i.e. all 1’s ~a gives 11000
~ (ones) present in the number are changed to
0’s (zeroes) and all the 0’s (zeroes) are
changed to 1’s (ones).

It shifts each bit in the operand to the left. The a<<3 gives 11000
<< number of places the bits are shifted depends
on the number following the operand.

It shifts each bit in the operand to the right. a>>2 gives 00001
>> The number of places the bits are shifted
depends on the number following the
operand.
TERNARY OPERATOR
Ternary operators are those operators
that operate on three operands. It is also
called as conditional operator. The ternary
operator acts like a shorthand version of the
if-else construct.
Syntax
variable = exp1?exp2 : exp3;
If exp1 is true, exp2 is evaluated,
otherwise exp3 is evaluated.
Example
Large = (a>b) ? a : b;
SPECIAL OPERATORS
, Links the related
expressions together
. Direct member selection
→ Indirect member selection
sizeof () It returns the size of a
variable
1 MARK QUESTIONS
1)What is an operator?
2)What is an operand?
3)What is meant by unary operator?
4)Mention the different unary
operators.
5)What is a binary operator?
6)Mention the different binary
operators.
7)Mention the different arithmetic
operators.
8)What is the difference between /
11)When is logical operator used?
12)Mention the different logical operators.
13)What is the purpose of assignment operator?
14)Write the syntax of assignment operator.
15)What is the purpose of the short-hand
operator?
16)What is the purpose of the bitwise operator?
17)Mention the different bitwise operators.
18)What is ternary operator?
19)What is the other name for the ternary
operator?
20)Write the syntax of ternary operator.
3 MARK QUESTIONS
1) Explain logical operators.
2) Explain assignment operator.
3) Explain ternary operator.
5 MARK QUESTIONS
4) Explain unary operators.
5) Explain arithmetic operators.
6) Explain relational operators.
7) Explain bitwise operators.

You might also like