AppDividend
Latest Code Tutorials

C++ Operators Tutorial With Example | Operator in C++ With Program

0

C++ Operators Tutorial With Example | Operator in C++ With Program is today’s topic. Before starting operation, we first need to learn what operation is? The Operation is a task to be performed given by the user. Now, this operation is performed using…

  1. Operand: An object (variable) that are manipulated
  2. Operator: A symbol that is used to perform an action/task with those operands.

C++ Operators Tutorial With Example

Operators are one of the building blocks of any programming language. Thus the functionality of C/C++ programming language is incomplete without the use of operators. C++ operator is the symbol that is used to perform mathematical or logical manipulations. C++ language is rich with built-in operators. Operators are a special type of functions, that takes one or more arguments and produces a new value. For example : addition (+), subtraction (-), multiplication (*) etc, are all operators. Operators are used to performing various operations on variables and constants. See the following figure of C++ Operators Tutorial.

 

C++ Operators Tutorial With Example

In the above figure, A, X, and Y is Operand and + and = are operators.

The addition operator tells the compiler to add both of the operands ‘X’ and ‘Y.’

Different Types of Operators

 

Different Types of Operators

Assignment Operator (=)

Assignment operator in C++ is used to assign a value to a variable. The symbol of this operator is ‘=.’

For Example,  int a=10;

Here, 10 will be assigned to the int type variable a.

Please note that the value which we want to assign will be at the right side of ‘=’ and the variable to which we want to assign the value will be at the left side of ‘=.’

Example 2: x = y;

Here, y value will be assigned to x, and later if we change the value of y, it will not effect on x.

We can assign a value to multiple variables at the same time also.

x = y = z = 89

See the following code example.

#include<bits/stdc++.h>
using namespace std;
int main()
{
	//we will assign the value of
	//x to y
	int x,y;
	cout<<"Enter value of x: ";
	cin>>x;
	y=x; //assigning the value of x to y
	cout<<"Value of x is: "<<x<<" value of y is "<<y;
	return 0;
}

See the output below.

Assignment Operator

 

Compound Assignment Operator

Compound Assignment operators are also used to assign value quickly.

For example: number1 += y

Here, the above statement is equivalent to number1 = number1 + y

So, why we need this? We can see that, and we will have to write the large variable name twice if we write it without the compound assignment operator, which is time-consuming. But, using Compound Assignment operator, we can write it in short.

There are few Compound Operator, and we will see some of those.

Expression Description
a+=b a=a+b
a-=b a=a-b
a^=b a=a^b
sum+=x*y sum=sum+(x*y)

 

See the below code example of Compound Assignment Operator in C++.

#include<bits/stdc++.h>
using namespace std;
int main()
{
	int x;
	cout<<"Enter value of x: ";
	cin>>x;
	x+=10; //x=x+10
	cout<<"New value of x: "<<x<<"\n";
	x*=2; //x=x*2
	cout<<"New value is: "<<x<<"\n";
	return 0;
}

See the output below.

Compound Operator

 

 

Arithmetic Operator

Arithmetic Operator is used to performing a mathematical operation.  There is 5 arithmetic operator in C++; they are following.

Operator Description
+ Used to perform the addition of two or more than two number.
Used to perform subtraction of two number.
* Used to perform multiplication.
/ Used to perform division
% Used to find modulo (reminder) of two number.

 

See the below Arithmetic operator in C++ example.

#include<bits/stdc++.h>
using namespace std;
int main()
{
	int x,y;
	cout<<"Enter value of x: ";
	cin>>x;
	cout<<"Enter value of y: ";
	cin>>y;
	cout<<"Addition of x and y is: "<<x+y<<"\n";
	cout<<"Subtraction of x and y is: "<<x-y<<"\n";
	cout<<"Modulo of x and y is: "<<x%y;
	return 0;
}

See the output.

Arithmetic Operator

 

Unary Operator

There are two of the unary operator in C++. One is for increment (++), and another is for decrement (–).

  1. ++: This means the operand’s value will be incremented by 1.
  2. –: This implies the operand’s value will be decremented by 1 If these operators are written before the variable (++X), then it will first. Increment the value then will perform any operation, but if this operator is placed after the variable (X++), then it will first perform the operation (if any) and then will increment the value.

See the example of Unary Operator in C++.

#include<bits/stdc++.h>
using namespace std;
int main()
{
	int x,y;
	cout<<"Enter value of x: ";
	cin>>x;
	cout<<"Enter value of y: ";
	cin>>y;
	//the value of x will show same
	//but it will be incremented
	cout<<"After increment the value of x is: "<<x++<<"\n";
	cout<<"After decrement the value of y is: "<<--y<<"\n";
	cout<<"Now the value of x is: "<<x;
	return 0;
}

See the following output.

 

Unary Operator

Relational Operator

Relational operators are used to compare two expressions, like if they are equal, greater than or less than, etc. The relational operators in C++ are following.

Operator Description
== Is equal to
!= Is not equal to (! represents not always)
> Is greater than
< Is less than
>= Is greater than or equal to
<= Is less than or equal to

 

Note: ‘=’ and ‘==’ are not the same. ‘=’ to assign value and ‘==’ is to compare the values.

These operators establish the relationship between operands. The relational operators are less than (<), greater than (>), less than or equal to (<=), greater than equal to (>=), equivalent (==) and not equivalent (!=).

You must notice that the assignment operator is (=) and there is the relational operator, for equivalent (==). These two are different from each other; the assignment operator assigns the value to any variable, whereas an equivalent operator is used to comparing values, like in if-else conditions.

See the following code example of Relational Operator in C++.

#include<bits/stdc++.h>
using namespace std;
int main()
{
	int x,y;
	cout<<"Enter value of x: ";
	cin>>x;
	cout<<"Enter value of y: ";
	cin>>y;
	if(x==y)
	{
		cout<<"X is equal to Y\n";
	}
	else if (x>y)
	cout<<"X is greater than or Y\n";
	else if (x<y)
	cout<<"X is less than Y\n";
	return 0;
}

See the below output.

Relational Operator

 

Logical Operator

Logical operators are used to performing boolean operations (AND, OR and NOT). If two statement are connected using an AND operator, then the validity of both statements will be considered, but if they are connected using an OR operator, then either one of them must be valid operator. These operators are mostly used in loops (especially while loop) and Decision making. We will learn in details. 

Operator Explanation
! Performs boolean NOT operation. It returns TRUE if the operand is False and returns FALSE if the operand is True.
&& Performs boolean AND operation. If both the operands are true, it returns TRUE otherwise it returns False.
|| Performs boolean OR operation. If both the operands are then only it returns False, otherwise it returns True.

 

See the following code example of Logical Operator in C++.

#include<bits/stdc++.h>
using namespace std;
int main()
{
	//here we will check if 
	// x is divided by 10 or
	//y is divided by 5
	//it will return true
	int x,y;
	cout<<"Enter value of x: ";
	cin>>x;
	cout<<"Enter value of y: ";
	cin>>y;
	
	// logical OR operator is used here
	if(x%10==0 || y%5==0)
	{
		cout<<"True\n";
	}
	return 0;
}

See the following output.

Logical Operator

 

Bitwise Operator

The bitwise operator is used to perform bit-level operations on the operands. The operators are first converted to bit-level, and then the calculation is performed on the operands.

Bitwise operators are:

Operator Explanation
& Is used to perform bitwise AND operation.
| Is used to perform bitwise OR operation.
^ Is used to perform bitwise XOR operation
~ Is used for unary complement (bit inversion)
<< Is used for binary Left Shift Operation
>> Is used for binary Right Shift Operation

See the following code example of Bitwise Operator in C++.

#include<bits/stdc++.h>
using namespace std;
int main()
{
	int x,y;
	cout<<"Enter value of x: ";
	cin>>x;
	cout<<"Enter value of y: ";
	cin>>y;
	cout<<"Bitwise AND of X and Y is: "<<(x&y)<<"\n";
	cout<<"Bitwise OR of X and Y is: "<<(x|y)<<"\n";
	cout<<"Shift Right of X: "<<(x>>1)<<"\n";
	return 0;
}

See the following output.

Bitwise Operator

 

Shift Operators

Shift Operators are used to shifting Bits of any variable. It is of three types,

  1. Left Shift Operator <<
  2. Right Shift Operator >>
  3. Unsigned Right Shift Operator >>>>

sizeof(type/variable) Operator

The sizeof() operator is used to find the size of a type or a variable. It returns the size in byte.

It accepts one parameter either it can be the variable type or a variable.

Example: s=sizeof(char)

This will return 1, as the size of char is 1 byte.

Conditional ternary operator

The conditional operator is used to get the result of one condition.

The syntax is: condition? result1: result2

Here, if the condition is true, then it will return result1; otherwise, it will return result2. It is similar to the if-else statement.

See the following code example of Conditional Operator in C++ Example.

#include<bits/stdc++.h>
using namespace std;
int main()
{
	int x,y;
	cout<<"Enter value of x: ";
	cin>>x;
	cout<<"Enter value of y: ";
	cin>>y;
	x>y?cout<<"Greater":cout<<"Smaller";
	return 0;
}

See the following output.

Conditional ternary operator

 

Comma Operator (,)

The comma operator (,) is used to separate the two or more expressions that are included where only one expression is expected. It returns a rightmost expression when there is multiple expression.

For Example: x=(m=10, m-5);

Here, the result will be 5, because, it will first assign the value of m to 10, and then it will subtract 5 from 10 (10-5) which results in 5, and then 5 will be assigned to x.

See the following code example of Comma Operator in C++.

#include<bits/stdc++.h>
using namespace std;
int main()
{
	int x,m;
	x=(m=10,m+7);
	cout<<"Value of x is: "<<x;
	return 0;
}

See the output below.

Comma Operator in C++

 

Finally, C++ Operators Tutorial With Example | Operator in C++ With Program post is over.

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.