Operators:
1. Assignment operator
2. Arithmetic operator
3. Logical operator
4. Bitwise operator
5. Increment and decrement operator
6. Short hand operator
1. Assignment operator:
The assignment operator assigns a value to a variable.
x = 5;
This statement assigns the integer value 5 to the variable x. The assignment operation always takes place from right to left, and never the other way around:
x = y;
This statement assigns to variable x the value contained in variable y. The value of x at the moment this statement is executed is lost and replaced by the value of y.
//Assignmentoperator.cpp
#include <iostream>
using namespace std;
int main ()
{
int a, b; // a:?, b:?
a = 10; // a:10, b:?
b = 4; // a:10, b:4
a = b; // a:4, b:4
b = 7; // a:4, b:7
cout << "a:";
cout << a;
cout << " b:";
cout << b;
}
2. Arithmetic operators ( +, -, *, /, % ):
The five arithmetical operations supported by C++ are:
operator description
======== ===========
+ - addition
- - subtraction
* - multiplication
/ - division
% - modulo
Operations of addition, subtraction, multiplication and division correspond literally to their respective mathematical operators. The last one, modulo operator, represented by a percentage sign (%), gives the remainder of a division of two values.
//Arithmaticoperator.cpp
#include<iostream>
using namespace std;
int main() {
int a,b;
cout<<"\n Enter the values of a and b"<<endl;
cin>>a>>b;
cout<<"The added value is "<<a+b<<endl;
cout<<"The subtracted value is "<<a-b<<endl;
cout<<"The multiplied value is "<<a*b<<endl;
cout<<"The divided value is "<<a/b<<endl;
cout<<"The modulo value is "<<a%b<<endl;
return 0;
}
3. Compound assignment (+=, -=, *=, /=, %=, >>=, <<=, &=, ^=, |=):
Compound assignment operators modify the current value of a variable by performing an operation on it. They are equivalent to assigning the result of an operation to the first operand:
expression equivalent to...
========== ================
y += x; y = y + x;
x -= 5; x = x - 5;
x /= y; x = x / y;
price *= units + 1; price = price * (units+1);
and the same for all other compound assignment operators.
// compound assignment operators
#include <iostream>
using namespace std;
int main ()
{
int a, b=3;
a = b;
a+=2; // equivalent to a=a+2
cout << a;
return 0;
}
4. Increment and decrement (++, --):
Some expression can be shortened even more: the increase operator (++) and the decrease operator (--) increase or reduce by one the value stored in a variable. They are equivalent to +=1 and to -=1, respectively. Thus:
1. ++x;
2. x+=1;
3. x=x+1;
are all equivalent in its functionality; the three of them increase by one the value of x.
In the early C compilers, the three previous expressions may have produced different executable code depending on which one was used. Nowadays, this type of code optimization is generally performed automatically by the compiler, thus the three expressions should produce exactly the same executable code.
A peculiarity of this operator is that it can be used both as a prefix and as a suffix. That means that it can be written either before the variable name (++x) or after it (x++). Although in simple expressions like x++ or ++x, both have exactly the same meaning; in other expressions in which the result of the increment or decrement operation is evaluated, they may have an important difference in their meaning: In the case that the increase operator is used as a prefix (++x) of the value, the expression evaluates to the final value of x, once it is already increased. On the other hand, in case that it is used as a suffix (x++), the value is also increased, but the expression evaluates to the value that x had before being increased. Notice the difference:
Example 1 Example 2
=====================================================
x = 3; x = 3;
y = ++x; y = x++;
// x contains 4, y contains 4 // x contains 4, y contains 3
//Incrementoperator.cpp
#include<iostream>
using namespace std;
int main() {
int i;
cout<<"\n Enter the value of i"<<endl;
cin>>i;
if(i==2){
i++;
cout<<"\n THE VALUE OF I IS"<<i<<endl;
}
else{
cout<<"\n I VALUE NOT INCREASED"<<endl;
}
}
//decrementoperator.cpp
#include<iostream>
using namespace std;
int main() {
int i;
cout<<"\n Enter the value of i"<<endl;
cin>>i;
if(i==5){
i--;
cout<<"\n THE VALUE OF I IS"<<i<<endl;
}
else{
cout<<"\n I VALUE NOT DECREASED"<<endl;
}
}
5. Relational and comparison operators ( ==, !=, >, <, >=, <= ):
Two expressions can be compared using relational and equality operators. For example, to know if two values are equal or if one is greater than the other.
The result of such an operation is either true or false (i.e., a Boolean value).
The relational operators in C++ are:
operator description
===========================
== Equal to
!= Not equal to
< Less than
> Greater than
<= Less than or equal to
>= Greater than or equal to
Here there are some examples:
(7 == 5) // evaluates to false
(5 > 4) // evaluates to true
(3 != 2) // evaluates to true
(6 >= 6) // evaluates to true
(5 < 5) // evaluates to false
Of course, it's not just numeric constants that can be compared, but just any value, including, of course, variables. Suppose that a=2, b=3 and c=6, then:
(a == 5) // evaluates to false, since a is not equal to 5
(a*b >= c) // evaluates to true, since (2*3 >= 6) is true
(b+4 > a*c) // evaluates to false, since (3+4 > 2*6) is false
((b=2) == a) // evaluates to true
Be careful! The assignment operator (operator =, with one equal sign) is not the same as the equality comparison operator (operator ==, with two equal signs); the first one (=) assigns the value on the right-hand to the variable on its left, while the other (==) compares whether the values on both sides of the operator are equal. Therefore, in the last expression ((b=2) == a), we first assigned the value 2 to b and then we compared it to a (that also stores the value 2), yielding true.
6. Logical operators ( !, &&, || ):
The operator ! is the C++ operator for the Boolean operation NOT. It has only one operand, to its right, and inverts it, producing false if its operand is true, and true if its operand is false. Basically, it returns the opposite Boolean value of evaluating its operand. For example:
!(5 == 5) // evaluates to false because the expression at its right (5 == 5) is true
!(6 <= 4) // evaluates to true because (6 <= 4) would be false
!true // evaluates to false
!false // evaluates to true
The logical operators && and || are used when evaluating two expressions to obtain a single relational result. The operator && corresponds to the Boolean logical operation AND, which yields true if both its operands are true, and false otherwise. The following panel shows the result of operator && evaluating the expression a&&b:
&& OPERATOR (and):
==================
a b a && b
true true true
true false false
false true false
false false false
The operator || corresponds to the Boolean logical operation OR, which yields true if either of its operands is true, thus being false only when both operands are false. Here are the possible results of a||b:
|| OPERATOR (or):
=================
a b a || b
true true true
true false true
false true true
false false false
Please Subscribe and follow Programming languages join now or Subscribe!!
Please follow the notes given here and Share this Blog. For any comments or any mistakes report or comment in comment section of this blog.
ALL THE BEST!!!
0 Comments