Operators in C++

In programming, operators are the symbols that perform mathematical or logical operations on one or more values. To fulfill any operation, we require two important things one is an operator and the second is an operand. 


Example: int a = b + c;

Here, + is the operator, and b and c are the operands on which the addition operation is going to perform. 


Below is the list of operators available in the C++ programming language.

list of operators

1. Arithmetic Operators.

All Arithmetic Operators are binary operators which means they required two operands to perform operations. They are used to perform arithmetic or mathematical operations on operands. Below is the list of Arithmetic Operators in C++:
Operator Operation
+ Addition Operator
- Subtraction Operator
* Multiplication Operator
/ Division Operator
% Modulo Operator (Remainder)

C++ Example code:
//C++ Example of Arithmetic Operation
#include<iostream>
using namespace std;

int main(){
    
    int x = 10, y = 2;

    //Addition operator
    cout<<"x + y = "<<(x + y)<<endl;

    //Subtraction operator
    cout<<"x - y = "<<(x - y)<<endl;

    //Multiplication operator
    cout<<"x * y = "<<(x * y)<<endl;

    //Division operator
    cout<<"x / y = "<<(x / y)<<endl;

    //Modulo operator
    cout<<"x % y = "<<(x % y)<<endl;

    return 0;
}
Output:
x + y = 12
x - y = 8
x * y = 20
x / y = 5
x % y = 0

It might be possible that in some mathematical expression, more than one operators are present and in that case, we need to follow Operator Precedence and Associativity rules to evaluate the given expression. 
Precedence Operators Associativity
Highest *, /, % Left to Right
Lowest +, - Left to Right

To solve a mathematical expression, operators with higher precedence need to be solved first then the lower precedence. Associativity is used only when two or more operators are of same precedence. Let's understand this with C++ example code.
//C++ Example of Operation Precedence and Associativity
#include<iostream>
using namespace std;

int main(){
    
    int a = 2, b = 4, c = 6, d = 8;

    /*
    all operators are of same precedence so
    follow associativity rules 
    (solve the expression from Left to Right)
    */ 
    cout<<"a * b / c = "<<(a * b / c)<<endl;

    /*
    operators with high precedence like * and % 
    will be solve first then operators with low 
    precedence will be solve
    */
   cout<<"a + b * d - c % a = "<<(a + b * d - c % a)<<endl;

    return 0;
}
Output:
a * b / c = 1
a + b * d - c % a = 34
Note: All Operators are not use with all data types like modulo operator (%) cannot be used with floating data type. (alert-warning)

2. Increment / Decrement Operators. 

  • The Increment operator (++) is used to increment the value of a variable by one. 
  • The Decrement operator (--) is used to decrement the value of a variable by one.
Both increment and decrement operators are unary operators which means they required only operand to perform operation. 

Increment/Decrement operators are divided into different categories. 
Operator Operation Description
++a Pre-increment Operator The value of the variable is incremented first and then it is used
a++ Post-increment Operator The value of the variable is assigned first and then it is incremented
--a Pre-decrement Operator The value of the variable is decremented first and then it is used
a-- Post-decrement Operator The value of the variable is assigned first and then it is decremented

C++ Example code:
//C++ Example of Increment/Decrement Operators
#include<iostream>
using namespace std;

int main(){
    
    int a = 6, b = 10, c = 8, d = 2;

    //pre-increment
    cout<<"++a = "<<++a<<endl;

    //post-increment
    cout<<"b++ = "<<b++<<endl;
    cout<<"b = "<<b<<endl;
//pre-decrement cout<<"--c = "<<--c<<endl; //post-decrement cout<<"d-- = "<<d--<<endl;
    cout<<"d = "<<d<<endl;
return 0; }
Output:
++a = 7
b++ = 10
b = 11
--c = 7
d-- = 2
d = 1

In the above example, you can observe that ++a has increased the value by one, and then it is printed but b++ has assigned the same value to be at first, and then when we try to print it again then it got updated with an incremented value. A similar case is happening with decrement operators. 

3. Relational Operators.

Relational operators are used to compare two operands and return a boolean value either True or False based on the condition. 
Operator Operation
== equal to
!= not equal to
<= less than or equal to
>= greater than or equal to
< less than
> greater than
 
Example:
int a = 5, b = 4;
bool check = a > b;
In this example, > greater than relational operator is used to check if a is greater than b or not. If the given condition is true then it will return 1 and if the condition fails then it will return 0. 

C++ Example code:
//C++ Example of Relational Operators
#include<iostream>
using namespace std;

int main(){

    int a = 5, b = 10;
    bool check;

    //equal to
    check = (a == b);
    cout<<"a == b is "<<check<<endl;

    //not equal to
    check = (a != b);
    cout<<"a != b is "<<check<<endl;

    //less than or equal to
    check = (a <= b);
    cout<<"a <= b is "<<check<<endl;

    //greater than or equal to
    check = (a >= b);
    cout<<"a >= b is "<<check<<endl;

    //less than
    check = (a < b);
    cout<<"a < b is "<<check<<endl;

    //greater than
    check = (a > b);
    cout<<"a > b is "<<check<<endl;

    return 0;
}
Output:
a == b is 0
a != b is 1
a <= b is 1
a >= b is 0
a < b is 1
a > b is 0

4. Logical Operators.

Logical operators are used to combining two more conditions together and return true or false if conditions pass or fail. There are three logical operators in C++ programming mostly use for decision-making. 
Operator Operation Description
&& Logical AND Return True when all the conditions under consideration are true and Return False when any one or more conditions are false.
|| Logical OR Return True when one or more than one condition under consideration is true and Return False when all conditions are false.
! Logical NOT Return True when a condition is false and Return False when a condition is True.
//C++ Example of Logical Operators
#include<iostream>
using namespace std;

int main(){

    int a = 5, b = 10, c = 4;
    bool check;

    //AND 
    check = (a > b) && (a > c);
    cout<<"Expression1 && Expression1 is "<<check<<endl;

    //OR
    check = (a > b) || (a > c);
    cout<<"Expression1 || Expression1 is "<<check<<endl;

    //NOT
    check = (!(b > c));
    cout<<"!(Expression) is "<<check<<endl;

    return 0;
}
Output:
Expression1 && Expression1 is 0
Expression1 || Expression1 is 1
!(Expression) is 0
  • (5 > 10) is false and (5 > 4) is true, as we used logical AND (&&) which means if any one condition is false it will return False (0).
  • (5 > 10) is false and (5 > 4) is true, as we used logical OR (||) it means even if any one condition is true it will return True (1). 
  • (10 > 4) is true and we used logical NOT so it will always return the opposite that is false.

5. Bitwise Operators.

Bitwise operators are used to performing calculations on a bit level. You can use bitwise operators only with integer or character data types. There are six bitwise operators in C++ programming.

Operator Operation Description
& Bitwise AND It is a binary operator that performs bitwise AND operation. The result of AND is 1 when both bits are 1.
| Bitwise OR It is a binary operator that performs a bitwise OR operation. The result of OR is 0 when both bits are 0.
~ Bitwise NOT It is a unary operator that performs a complement of each bit one by one. The result of NOT is 0 when a bit is 1 and 1 when a bit is 0.
<< Left Shift It shifts the value to the left by the number of bits specified by the right operand.
>> Right Shift It shifts the value to the right by the number of bits specified by the right operand.
^ Bitwise XOR It results in 1 if both the inputs are not the same otherwise result is 0.

6. Assignment Operators.

Assignment operators are used to assigning value to a variable. Below is the list of assignment operators in C++. 
Operator Description
+= First addition and then assignment.
-= First subtraction and then assignment.
*= First multiplication and then assignment.
%= First modulus and then assignment.
<<= First bitwise left shift then assignment.
>>= First bitwise right shift then assignment.
&= First bitwise AND then assignment.
|= First bitwise OR then assignment.
^= First bitwise XOR then assignment.

Example:
int a = 10;
a += 5; //equivalent to a = a + 5 = 10 + 5 = 15

C++ Example code:
//C++ Example of Assignment Operators
#include<iostream>
using namespace std;

int main(){

    int a = 5, b = 10, c = 4;
    bool check;

    a += b; // equivalent to a = a + b
    cout<<"a = "<<a<<endl;

    c *= b; //equivalent to c = c * b
    cout<<"c = "<<c<<endl; 

    return 0;
}
Output:
a = 15
c = 40

7. Ternary Operator.

Ternary operator (?) which is also known as a Conditional operator accepts three arguments and returns a value based on the condition. Let's understand with some examples.
Syntax: Expression1 ? Expression2 : Expression3;
Here if expression1 return true then expression2 will get evaluated to get our answer and if the expression1 return false then expression3 will get evaluated to get our answer. (alert-passed)

 C++ Example code:

//C++ Example of Conditional Operators
#include<iostream>
using namespace std;

int main(){

    int a = 5, b = 10;
    //ternary operator
    int result = (a < b) ? a : b;
    cout<<"The smallest number: "<<result<<endl;

    return 0;
}
Output:
The smallest number: 5


There are a few more useful operators in C++ and here we are going to list a few of them that are more commonly used. 
Operator Description
sizeof() It is a unary operator used to calculate the size of a variable. Example: int size = sizeof(int); // output 4
& It is used to represent the memory address of a variable or operand.
-> It is used to access the variable of class or structure.
. (Dot Operator) It is used to access members of structure variables or class objects.

⚡ Please share your valuable feedback and suggestion in the comment section below or you can send us an email on our offical email id ✉ algolesson@gmail.com. You can also support our work by buying a cup of coffee ☕ for us.

Similar Posts

No comments:

Post a Comment


CLOSE ADS
CLOSE ADS