Software Developer
Right here, let us start with the aid of understanding approximately mission operators in c++. Because the call already suggests, those operators assist in assigning values to variables. We talk about these concerning operators and operands. These operators assist us in allocating a selected fee to the operands.
The primary easy mission operator is ‘=’. We ought to make sure that both the left and proper facets of the operator should have the same records type. We've extraordinary tiers of operators. Let us find out about each of those in c++. Undertaking operators are part of binary operators. Examples for these are: =, +=, -=, *=, /=, %=. Let us find out about each of these with examples.
There are three levels of operators.
Assignment operators are a part of binary operators.
Examples for these are: =, +=, -=, *=, /=, %=. Let us learn about each of these with examples.
Let us start with the first example with the ‘=’ operator as given below.
#include <iostream>
using namespace std;
int main()
{
int a,b;
char c;
float d;
a=10;
b=10.5;
c='R';
d=5.85;
cout<<" Value of a is: "<<a<<endl;
cout<<" Value of b is: "<<b<<endl;
cout<<" Value of c is: "<<c<<endl;
cout<<" Value of d is: "<<d<<endl;
}
So, as an example, we can take into account that the assignment operator ‘=’ is just going to assign values based on the statistics type the usage of the operands and variables. Above, we can see that the cost b that is given the facts type of int, offers best the price till the decimal factor. Simplest if we deliver the statistics kind drift just like the variable d, the complete cost receives displayed. So, we will say that records kind also play a vital position in placing and showing the values of different operands primarily based on their values.
In this example let us learn how ‘+=’ this operand works.
#include <iostream>
using namespace std;
int main()
{
int a,b,c;
a=10;
b=5;
c=5;
a+=6;
b+=20;
c=c+20;
cout<<" Value of a is: "<<a<<endl;
cout<<" Value of b is: "<<b<<endl;
cout<<" Value of c is: "<<c<<endl;
}
As observed above, the instance of ways we were given the cost for variable c, is the method of ways the operator ‘+=’ works. As consistent with the operand, first, the left aspect operand is introduced to the left aspect operand and then the very last price is being assigned to the left facet operand. That is how ‘+=’ is used.
Now, let us learn about the operand ‘-=’. This is almost similar to that of ‘+=’. The above operand adds the value, but here it gets subtracted. Let us see an example below.
#include <iostream>
using namespace std;
int main()
{
int a,b,c;
a=10;
b=5;
c=5;
a-=6;
b-=20;
c=c-4;
cout<<" Value of a is: "<<a<<endl;
cout<<" Value of b is: "<<b<<endl;
cout<<" Value of c is: "<<c<<endl;
}
Here also we have given example for variable ‘c’ on how the assignment of value is done. First, the left side operand value subtracts the right-hand value from it. In this process, there might be a negative value obtained.
Here let us have an example for the assignment operator *=, /= and %= together.
#include <iostream>
using namespace std;
int main()
{
int a,b,c;
a=10;
b=20;
c=25;
a*=2;
b/=5;
c%=10;
cout<<" Value of a is: "<<a<<endl;
cout<<" Value of b is: "<<b<<endl;
cout<<" Value of c is: "<<c<<endl;
}
Now let us look at how operators &=, ^= and |= as shown below.
#include <iostream>
using namespace std;
int main()
{
int a,b,c;
a=5;
b=6;
c=7;
a&=3;
b^=3;
c|=3;
cout<<" Value of a is: "<<a<<endl;
cout<<" Value of b is: "<<b<<endl;
cout<<" Value of c is: "<<c<<endl;
}
Here, let us learn about the operators <<= and >>= as given below.
#include <iostream>
using namespace std;
int main()
{
int a,b;
a=5;
b=5;
a>>=1;
b<<=1;
cout<<" Value of a is: "<<a<<endl;
cout<<" Value of b is: "<<b<<endl;
}