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
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:
- Arithmetic operator
- Increment/decrement operator
- Relational operator
- Logical operator
- Assignment operator
- 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
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.
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
|==||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-side||A<B is true|
|>||Gives the result as true if the variable on the left-hand-side is greater than that of the right-hand-side||A>B is false|
|<=||Gives the result as true if the left-hand-side variable is less than or equal to the right-hand-side variable||A<=B is true|
|>=||Gives the result as true if the left-hand-side variable is greater than or equal to the right-hand-side variable||A>=B is false|
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.
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.
|A||B||A && B|
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.
|A||B||A || B|
- ( 5<3 && 6!=4) Answer= False
- ! (4==5) Answer= True
- (4<6 || 7>3 && 5!=6) Answer= True
Assignment operators are used to assigning the value to the variable.
|=||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.|
|sizeof()||it gives the size of the variable.||If A is a char variable, then the sizeof(A) will be 1.|
|Condition?X:Y||If 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 (||)|
|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.