C++ Operators

C++ have many built-in operators. Operators are symbols that tells the compiler to perform a specific operation. Without operators C++ programming language is incomplete.

Here we will discuss following types of operators;

  • Arithmetic operators
  • Assignment operators
  • Logical operators
  • Relational operators
  • Operators Precedence

Arithmetic Operators

Arithmetic operators are used to perform mathematical operations on operands.

For example:

a + b;

Here + is an operator. Same as -, /, *, %, etc are operators.

Operators, their description and examples

+ => adds two operands. e.g: a+b;

– => substrates two operands. e.g: a-b;

/ => divides two operands. e.g: a/b;

* => multiplies two operands. e.g: a*b;

% => modulus of two operands. It gives the remainder of two integers after their division. e.g: a%b;

++ => increment operator. Increase the value one by one. e.g: a=5; after increment it will b 6, a=6;

— => decrement operator. Decrease the value one by one. e.g: a=5; after decrement it will be 4, a=4;

Assignment Operator

Assignment operators are use to assigne values to the variables.

For example:

  • x = 5;

Here = is the simplest assignment operator, x is a variable and 5 is the value that is assigned to x with the help of this assignment operator.

  • a += b;

Here two operators are combine add and assignment operator, that means firstly it adds the values of a and b then that value will be assigne to a.

  • a -= b;

Subtract and assignment operators are combine, firstly value of a subtracts from the value of b then the resultant value will be assigne to a.

This process will be same to other operators. For example *=, /=, %=, &=, ^=, >>=, <<=,  etc.

Logical Operators

These are use to combine Two or more conditions and the result of a logical operator will be in Boolean data type, either true or false.

Operators, their description and example

! => It is known as logical not operator. To reverse any condition logical not operator is used. e.g: if any condition is true logical not operator along this condition make is false.

&& => It is logical AND operator. When we want both the conditions true, then && operator is use. e.g: (a=5 && b=25) it’s mean program will be run only when the both conditions are true.

|| => It is logical OR operator. It is use when we want one of two condition true. e.g: (a=5 || b=7) if one of two condition is true program will run.

Relational Operators

These are used to compare the values of two operands. It checks whether the values of two operands are equal or not, value of one operand is less than other operand or not etc.

Operators, their description and example

  1. > It checks whether the value on left side is greater than the value on right side. e.g: a>b;
  2. >= It checks whether the value on left side is greater than or equals to the value on the right side. e.g: a>=b;
  3. < It checks whether the value on left side is less than the value on the right side. e.g: a<b;
  4. <= It checks whether the value on the left side is less than or equals to the value on the right side. e.g: a<=b;
  5. == It checks whether the values of two operands are equal or not. If they are equal then true. e.g: Suppose a==b is not true.
  6. != It checks whether the values of two operands are equal or not, and reverse the condition. e.g: a==b is false than a!=b is true.

Operands Precedence

Operators precedence is actually about the evaluation of operators. When more than one operator are used C++ use the precedence rule to decide which operator is use first. Because some operators have higher precedence than others.

For Example

a=5+3*8;

Here firstly multiply 3*8 than add 5 result should be 29 not 64 because multiplication operator has higher precedence than addition.

Multiplication also has the higher precedence than division operator and modulus operator.

Addition operator has higher precedence than subtraction operator.

Equal operator has higher precedence than not equal operator.