0% found this document useful (0 votes)
3 views

Operators in C++

Uploaded by

sambhavdwivedi48
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)
3 views

Operators in C++

Uploaded by

sambhavdwivedi48
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/ 10

Operators In C++

1. Operators in C++ :)
• An operator is a symbol that operates on a value to
perform specific mathematical or logical computations.

1. Unary Operators

2. Binary Operators

3. Ternary Operators
1.1 . Unary Operators :)
• These operators operate or work with a single operand.
For example: Increment(++) and Decrement(–)
Operators.
Name Symbol Description Example
Increases the integer int a = 15;
Increment Operator ++ value of the variable by
one a++; // returns 16

Decreases the integer int a = 15;


Decrement Operator — value of the variable by
one a–; // returns 14
1.2 Binary Operators:

1. Arithmetic Operator
2. Relational Operator
3. Logical Operator
4. Bitwise Operator
5. Assignment Operator
1.2.1 Arithmetic Operator :)
Name Symbol Description Example

int a = 3, b = 6;
Addition + Adds two operands
int c = a+b; // c = 9

Subtracts second operand from the int a = 9, b = 6;


Subtraction – first int c = a-b; // c = 3

int a = 3, b = 6;
Multiplication * Multiplies two operands
int c = a*b; // c = 18

Divides first operand by the second int a = 12, b = 6;


Division / operand int c = a/b; // c = 2

Returns the remainder an integer int a = 8, b = 6;


Modulo Operation % division int c = a%b; // c = 2
1.2.2 Relational Operator :)
Name Symbol Description Example

int a = 3, b = 6;
Is Equal To == Checks if both operands are equal a==b;
// returns false

int a = 3, b = 6;
Checks if first operand is greater than the
Greater Than > second operand a>b;
// returns false

int a = 3, b = 6;
Checks if first operand is greater than or
Greater Than or Equal To >= equal to the second operand a>=b;
// returns false

int a = 3, b = 6;
Checks if first operand is lesser than the
Less Than < a<b;
second operand
// returns true

int a = 3, b = 6;
Checks if first operand is lesser than or
Less Than or Equal To <= equal to the second operand a<=b;
// returns true

int a = 3, b = 6;
Not Equal To != Checks if both operands are not equal a!=b;
// returns true
1.2.3 Logical Operators :)
Name Symbol Description Example

Returns true only if all the int a = 3, b = 6;


Logical AND && operands are true or non- a&&b;
zero // returns true

Returns true if either of int a = 3, b = 6;


Logical OR || the operands is true or a||b;
non-zero // returns true

int a = 3;
Returns true if the
Logical NOT ! !a;
operand is false or zero
// returns false
1.2.4 Bitwise Operators :)
Name Symbol Description Example

Copies a bit to the evaluated int a = 2, b = 3;


Binary AND &
result if it exists in both operands (a & b); //returns 2

Copies a bit to the evaluated int a = 2, b = 3;


Binary OR | result if it exists in any of the
operand (a | b); //returns 3

Copies the bit to the evaluated int a = 2, b = 3;


Binary XOR ^ result if it is present in either of
the operands but not both (a ^ b); //returns 1

Shifts the value to left by the int a = 2, b = 3;


Left Shift << number of bits specified by the
right operand. (a << 1); //returns 4

Shifts the value to right by the int a = 2, b = 3;


Right Shift >> number of bits specified by the
right operand. (a >> 1); //returns 1

Changes binary digits 1 to 0 and 0 int b = 3;


One’s Complement ~
to 1 (~b); //returns -4
1.2.5 Assignment Operators
Namemultiply Symbol Description Example

Assigns the value on the right to the int a = 2;


Assignment Operator =
variable on the left // a = 2

First adds the current value of the


variable on left to the value on the int a = 2, b = 4;
Add and Assignment Operator +=
right and then assigns the result to the a+=b; // a = 6
variable on the left

First subtracts the value on the right


from the current value of the variable int a = 2, b = 4;
Subtract and Assignment Operator -=
on left and then assign the result to a-=b; // a = -2
the variable on the left

First multiplies the current value of


the variable on left to the value on the int a = 2, b = 4;
Multiply and Assignment Operator *=
right and then assign the result to the a*=b; // a = 8
variable on the left

First divides the current value of the


variable on left by the value on the int a = 4, b = 2;
Divide and Assignment Operator /=
right and then assign the result to the a /=b; // a = 2
variable on the left
1. 3 Ternary or Conditional
Operators(?:)
• Expression1 ? Expression2 : Expression3;

• The ternary operator ? determines the answer on the


basis of the evaluation of Expression1. If it is true,
then Expression2 gets evaluated and is used as the
answer for the expression. If Expression1 is false,
then Expression3 gets evaluated
• This operator takes three operands, therefore it is
known as a Ternary Operator.

You might also like