March 19, 2024
coding loop

Operators in C++- 6 types of operators with examples

Hey guys, what is up. In this particular post, we will be talking about what are operators in c++ and types of operators with examples.

Updated on 29/9/2021

KEY POINTS

OPERATORS IN C++

Operators in c++ are the symbols that triggers an action on variables and values to perform mathematical or logical operations. These are of 6 types:

  1. Arithmetic operator
  2. Increment/decrement operator
  3. Relational operator
  4. Logical operator
  5. Assignment operator
  6. Misc operator

Arithmetic operators in c++

It is the most important operator. It takes numerical values in the form of input and then produces a single numerical value after operating.

Example: A=4, B=2. If we want to perform addition and subtraction using these variables: A+B=6 & A-B=2. Here “+” and “–” are arithmetic

OPERATORSMEANINGEXAMPLERESULT
    
+Addition4+26
Subtraction4-22
*Multiplication4*28
/Division4/22
%Modulus4%20

Increment/decrement operators in c++

The increment operator (++) adds the value of a operand by 1.

Types of increment operators:

  • Pre – increment: It increases the value of the operand and then stores the new value of the operand.
  • Post – increment: It first stores the value of the operand and then increases the value of the operand.

The decrement operator (–) drops the value of the variable by 1.

Types of decrement operators:

  • Pre – decrement: it first decreases the value of the operand and then stores the new value of the operand.
  • Post – decrement: it first stores the value of the operand and then decreases the value of the operand.

EXAMPLE 1:

example 1 operators in c++

OUTPUT:

output 1 ques

EXAMPLE 2:

example 2 operators in c++

OUTPUT:

output 2 ques

Relational operators

Relational operators in c++ are used in defining the relationship between 2 operators. These operators compare the value and return true or false in the form of binary value. 

Let us understand this with an example taking A=8, B=9

OperatorOperationExample
==Gives result as true if values of 2 operands/variables are equal.A==B is not true
!=Gives result as true if values of 2 variables/operands are not equal.A != B is true
Gives the result as true if the variable on the  right-hand-side is greater than that of the left-hand-sideA<B is true
Gives the result as true if the variable on the left-hand-side is greater than that of the right-hand-sideA>B is false
<=Gives the result as true if the left-hand-side variable is less than or equal to the right-hand-side variableA<=B is true
>=Gives the result as true if the left-hand-side variable is greater than or equal to the right-hand-side variableA>=B is false

Logical operators

Whenever one or more than one expressions need to be combined then we use logical operators. We have 3 fundamental logical operators: ! (Not), && (And), || (Or).

Not (!): It is a unary operator. It reverses the value of the variable/operand.

A!A
10
01

And (&&): It is a binary operator which operates with 2 operands. Gives true if both the variables are non-zero. In simple words, it can be explained as the multiplication of values of both operands.

ABA && B
000
010
100
111

Or (||): It is also a binary operator which operates with 2 operands. Gives result as true when at least one of the operands is non-zero. In simple words, it is the addition of values of both operands.

ABA || B
000
011
101
111

Example:

  1. ( 5<3 && 6!=4)                   Answer= False
  2. ! (4==5)                               Answer= True
  3. (4<6 || 7>3 && 5!=6)          Answer= True

Assignment operators

Assignment operators are used to assigning the value to the variable.

OPERATOROPERATIONEXAMPLE
   
=Assigns the value of the right-hand-side to the left-hand-side.A=B. In this, value of B will be stored in A.
+=Adds RHS to the LHS and assigns the result to the left-hand-side.A+=B. In this, A=A+B.
-=Subtracts RHS from LHS and assigns the result to LHS.A-=B. In this, A=A-B.
*=Multiplies RHS with LHS and assigns the result to LHS.A*=B. In this, A=A*B.
/=Divides RHS with LHS and assigns the result to LHS.A/=B. In this, A=A/B.

Misc operators

OPERATOROPERATIONEXAMPLE
   
sizeof()it gives the size of the variable.If A is a char variable, then the sizeof(A) will be 1.
Condition?X:YIf the condition is true, then X will be executed otherwise Y will be executed.5>3? true:false. In this case, true will be printed.
Comma (,)It causes a sequence of operations. It is the value of the last expression of the comma-separated list. 

Precedence of operators:

Post increment (++) and post decrement (–)
(++) Pre-increment, (–) pre-decrement, sizeof, unary plus (+) and unary minus (-)
Multiplication (*), division (/) and modulus (%)
Addition (+) and subtraction (-)
Less than (<), Less than or equal (<=), Greater than (>) and greater than or equal (>=)
Equal (==) and not equal (!=)
Logical And (&&)
Logical Or (||)
Conditional (?:)
Assignment operators (=, +=, -=, *=, /=)
Comma operator (,)

In the upcoming post, you will be getting information related to big data analytics technology and Nesting of loops in c++. If you haven’t read about what are loops in C++ then go and read this. You can read previous posts by scrolling downwards. Also, stay tuned for the upcoming post.

Leave a Reply

Your email address will not be published. Required fields are marked *