Types of Operators in C++ programming

Spread the love

Operators are special symbols used for specific purposes. Operators are a special type of function, that takes one or more arguments and produces a new value. 

C++ Programming provides six types of operators.

INDEX –

  1. Types of Operators in C++ programming
  2. Unary operators
  3. Increment / Decrement Operator
  4. Binary Operators
  5. Arithmetic Operators
  6. Relational Operators
  7. Logical Operators
  8. Assignment Operators
  9. Bitwise Operators
  10. Ternary Operators
  11. Conditional Operators
  12. Conclusion
  13. Frequently asked questions

Types of Operators in  C++ programming

operators

1. Unary Operators –

Unary operators are called operators who perform tasks with single operand as increment decrement operators.

Ex: a++, a–, ++aa,–a

1. Increment and Decrement Operators –

ncrement and Decrement Operators

C++ provides two special operators  ‘++’ and ‘–‘ for incrementing and decrementing the value of a variable by 1.

The increment/decrement operator can be used with any type of variable but it cannot be used with any constant. Increment and decrement operators each have two forms, pre, and post.

Increment Operator (++) This increases the value of the variable by 1.

Decrement Operator (-) It reduces the value of the variable by 1.

The syntax of the increment operator is:

Pre-increment: ++variable

Post-increment: variable++

The syntax of the decrement operator is

Pre-decrement: – – variable

Post-decrement: variable – –

  OperatorsSame as
++a (Increment Prefix)a = a + 1
–a (Decrement Prefix)a = a – 1
a++ (Increment Postfix)
a– (Decrement Postfix)

In Prefix form the first variable is first incremented/decremented, then evaluated.

In Postfix form, first variable is the first evaluated, then incremented/decremented

2. Binary Operators –

Binary operators are called such operators who need two operands to perform the task. Such as: Arithmetic operator (+, -, *, /), Relational Operator (>, <,> =, <=) etc.

1. Arithmetic Operators –

Arithmetical operators +, -, *, /, and % are used to performs an arithmetic (numeric) operation. You can use the operators +, -, *, and / with both integral and floating-point data types.

Modulus or remainder % operator is used only with the integral data type.

Operators that have two operands are called binary operators.

Arithmetical operators
OperatorsExplanation
+ (Addition)It adds two operands.
 (Subtraction)This removes the left operand from the right operand.
* (Multiplication)It multiplies the two operands.
/ (Division)It divides left operand by right operand.
% (Modulus)It extracts the remainder by dividing the left operand by the right operand.

2. Relational Operators –

The relational operators are used to test the relation between two values. All relational operators are binary operators and therefore require two operands.

A relational expression returns zero when the relation is false and a non-zero when it is true.

The following table shows the relational operators.

relational operators
OperatorExplanation
< (less than)It returns true if the value of one operand is less than the other operand. for eg. num1 = 5; num2 = 6; num1 <num2
> (greater than)It returns true if the value of one operand is higher than the other operand. for eg. num1 = 6; num2 = 5; num1> num2
<= (less than or equal to)If the value of one operand is less than or equal to the other operand, it returns true. for eg. num1 = 5; num2 = 5;num1 <= num2
>= (greater than or equal to)If the value of one operand is greater than or equal to the other operand, it returns true. for eg. num1 = 5; num2 = 5; num1> = num2
== (equal to)When two operands are equal, then it returns true.
!= (not equal to)When two operands are separated from each other, it returns true.

3. Logical Operators –

The logical operators are used to combine one or more relational expressions.

LOGICAL operators

The logical operators are

OperatorsExplanation
&& (logical &&)If both the conditions are true then it will return true. for eg. (5 <6) && (6> 5)
|| (logical OR)If either one is true, then it will return true. for eg. (5 <6) || (6> 5)
! (logical not)If the condition is true, it makes it false. for eg. ! ((5 <6) && (6> 5)) ! ((5 <6) || (6> 5))

4. Assignment Operators –

The assignment operator ‘=’ is used for assigning a variable to a value. This operator takes the expression on its right-hand-side and places it into the variable on its left-hand-side.

For example – a = 5;

The operator takes the expression on the right, 5, and stores it in the variable on the left, a.x = y = z = 32;

This code stores the value 32 in each of the three variables x, y, and z.

In addition to standard assignment operator shown above, C++ also support compound assignment operators.

assignment operator

read also – data-types

There are eleven types of Assignment Operators

  1. Assignment Operator (=)
  2. Add Assignment Operator (+=)
  3. Subtract Assignment Operator (-=)
  4. Multiply Assignment Operator (*=)
  5. Divide Assignment Operator (/=)
  6. Modulus Assignment Operator (%=)
  7. Bitwise AND Assignment Operator (&=)
  8. OR Assignment Operator (|=)
  9. Bitwise XOR Assignment Operator (^=)
  10. Left Shift Assignment Operator (<<=)
  11. Right Shift Assignment Operator (>>=)
ExamplesExamples
= (assignment)c = a + b
+= (add assignment)c += a same as c = c + a
-= (subtract assignment)c -= a same as c = c – a
*= (multiply assignment)c *= a same as c = c * a
/= (divide assignment)c /= a same as c = c / a
%= (modulus assignment)c %= a same as c = c % a
&= (AND assignment)c &= a same as c = c & a
|= (OR assignment)c |= a same as c = c | a
^= (XOR assignment)c ^= a same as c = c ^ a
<<= (Left Shift assignment)c <<= a same as c = c << a
>>= (Right Shift assignment)c >>= a same as c = c >> a

5. Bitwise Operators –

There are six types of Bitwise Operators

1. Binary AND (&)

2. Binary OR (||)

3. Binary XOR (^)

4. Binary Ones Complement (^)

5. Binary Left Shift (<<)

6. Binary Right Shift (>>)

bitwise operator in c++

Logical operators only return true or false. It is also used to combine relational conditions. Here, Bitwise operators return their result keeping in view the binary values ​​of two variables.

Logical operators

In XOR (^) the binary value of numeric value where 1 is where it becomes 0 and where 0 is where it becomes for eg. b = 12; / * 0000 1100 * / Conversely, 1111 will become 0011.

Left Shift (<<) for e.g. a = 20; / * 0001 0100 * / a << Shifts every binary number from 2 binary numbers left side to binary value of numeric value in 2. for e.g.a = 20; / * 0001 0100 * / So its 0101 0000 means 80.

Right Shift (>>) for e.g. a = 20; / * 0001 0100 * / This is completely opposite from Left shift. Right Shift a >> shifts every binary number from the right side to 2 binary numbers in binary value of numeric value in 2. for e.g.a = 20; / * 0001 0100 * / So its 0000 0101 means 5.

3. Ternary Operators –

Ternary operators are called such operators who require three operands. It is used in decision making. Like <condition>? <true block>: <false block>;

Conditional operators –

The conditional operator ? : is called a ternary operator as it requires three operands.

The format of the conditional operator is

Conditional_ expression ? expression1 : expression2;

If the value of a conditional expression is true then the expression1 is evaluated, otherwise

expression2 is evaluated.

int a = 5, b = 6;

big = (a > b) ? a : b; The condition evaluates to false, therefore big gets the value from b and

it becomes 6.

Conditional Operator has three Expressions.

Conditional Operator is known as Ternary Operator.

In Conditional Operator, if the first expression is true, then it prints the second expression in output.

If the first expression in the Conditional Operator is false, then it prints the third expression in the output.

conditional operator in c++

Syntax for Conditional / Ternary Operator

expression 1? expression 2: expression 3

source – softwaretestinghelp

Conclusion –

Types of Operators in c++ programming, Unary operators, Binary Operators, Ternary Operator, and these types. all of them Syntax, examples, I gave you above.

Friends, reading this post completely, you must have understood Operators in c++ programming. you must have liked it. I try to describe to you in simple language, maybe you must have understood it in this post. I have covered Topics you do not need to read any other post. If you liked this article, then definitely share it with your friends. And there is some doubt. Then comment I will definitely answer your comment.

Frequently asked questions

1.What are Arithmetic Operators?

Arithmetical operators +, -, *, /, and % are used to performs an arithmetic (numeric) operation. You can use the operators +, -, *, and / with both integral and floating-point data types.

2. What is a logical operator in C++?

Logical Operators: Logical Operators are used to combining two or more conditions/constraints or to complement the evaluation of the original condition into consideration. The result of the operation of a logical operator is boolean value true or false.

3. What is the assignment operator in C++?

 Assignment Operators in C++ Assignment operators are used to assigning value to a variable. The left side operand of the assignment operator is a variable and the right side operand of the assignment operator is a value. This operator is used to assign the value on the right to the variable on the left.

4. What are Bitwise Operators used for?

Bitwise operators are used to performing manipulation of individual bits of a number. They can be used with any of the integral types (char, short, int, etc). They are used performing updates and query operations of the Binary indexed tree. This operator is a binary operator, denoted by ‘|’.

5. What is conditional operator in C++?

The conditional operator ? : is called a ternary operator as it requires three operands.

Leave a Comment