2. C++ Assignment Operators
In C++, assignment operators are used to assign values to variables. For example,
// assign 5 to a
a = 5;Here, we have assigned a value of 5 to the variable a.
| Operator | Example | Equivalent to |
|---|---|---|
= | a = b; | a = b; |
+= | a += b; | a = a + b; |
-= | a -= b; | a = a - b; |
*= | a *= b; | a = a * b; |
/= | a /= b; | a = a / b; |
%= | a %= b; | a = a % b; |
Example 3: Assignment Operators
#include <iostream>
using namespace std;
int main() {
int a, b;
// 2 is assigned to a
a = 2;
// 7 is assigned to b
b = 7;
cout << "a = " << a << endl;
cout << "b = " << b << endl;
cout << "\nAfter a += b;" << endl;
// assigning the sum of a and b to a
a += b; // a = a +b
cout << "a = " << a << endl;
return 0;
}Output
a = 2 b = 7 After a += b; a = 9
3. C++ Relational Operators
A relational operator is used to check the relationship between two operands. For example,
// checks if a is greater than b
a > b;Here, > is a relational operator. It checks if a is greater than b or not.
If the relation is true, it returns 1 whereas if the relation is false, it returns 0.
| Operator | Meaning | Example |
|---|---|---|
== | Is Equal To | 3 == 5 gives us false |
!= | Not Equal To | 3 != 5 gives us true |
> | Greater Than | 3 > 5 gives us false |
< | Less Than | 3 < 5 gives us true |
>= | Greater Than or Equal To | 3 >= 5 give us false |
<= | Less Than or Equal To | 3 <= 5 gives us true |
Example 4: Relational Operators
#include <iostream>
using namespace std;
int main() {
int a, b;
a = 3;
b = 5;
bool result;
result = (a == b); // false
cout << "3 == 5 is " << result << endl;
result = (a != b); // true
cout << "3 != 5 is " << result << endl;
result = a > b; // false
cout << "3 > 5 is " << result << endl;
result = a < b; // true
cout << "3 < 5 is " << result << endl;
result = a >= b; // false
cout << "3 >= 5 is " << result << endl;
result = a <= b; // true
cout << "3 <= 5 is " << result << endl;
return 0;
}Output
3 == 5 is 0 3 != 5 is 1 3 > 5 is 0 3 < 5 is 1 3 >= 5 is 0 3 <= 5 is 1
Note: Relational operators are used in decision-making and loops.
4. C++ Logical Operators
Logical operators are used to check whether an expression is true or false. If the expression is true, it returns 1 whereas if the expression is false, it returns 0.
| Operator | Example | Meaning |
|---|---|---|
&& | expression1 && expression2 | Logical AND. True only if all the operands are true. |
|| | expression1 || expression2 | Logical OR. True if at least one of the operands is true. |
! | !expression | Logical NOT. True only if the operand is false. |
In C++, logical operators are commonly used in decision making. To further understand the logical operators, let's see the following examples,
Suppose,
a = 5
b = 8
Then,
(a > 3) && (b > 5) evaluates to true
(a > 3) && (b < 5) evaluates to false
(a > 3) || (b > 5) evaluates to true
(a > 3) || (b < 5) evaluates to true
(a < 3) || (b < 5) evaluates to false
!(a < 3) evaluates to true
!(a > 3) evaluates to falseExample 5: Logical Operators
#include <iostream>
using namespace std;
int main() {
bool result;
result = (3 != 5) && (3 < 5); // true
cout << "(3 != 5) && (3 < 5) is " << result << endl;
result = (3 == 5) && (3 < 5); // false
cout << "(3 == 5) && (3 < 5) is " << result << endl;
result = (3 == 5) && (3 > 5); // false
cout << "(3 == 5) && (3 > 5) is " << result << endl;
result = (3 != 5) || (3 < 5); // true
cout << "(3 != 5) || (3 < 5) is " << result << endl;
result = (3 != 5) || (3 > 5); // true
cout << "(3 != 5) || (3 > 5) is " << result << endl;
result = (3 == 5) || (3 > 5); // false
cout << "(3 == 5) || (3 > 5) is " << result << endl;
result = !(5 == 2); // true
cout << "!(5 == 2) is " << result << endl;
result = !(5 == 5); // false
cout << "!(5 == 5) is " << result << endl;
return 0;
}Output
(3 != 5) && (3 < 5) is 1 (3 == 5) && (3 < 5) is 0 (3 == 5) && (3 > 5) is 0 (3 != 5) || (3 < 5) is 1 (3 != 5) || (3 > 5) is 1 (3 == 5) || (3 > 5) is 0 !(5 == 2) is 1 !(5 == 5) is 0
Explanation of logical operator program
(3 != 5) && (3 < 5)evaluates to 1 because both operands(3 != 5)and(3 < 5)are 1 (true).(3 == 5) && (3 < 5)evaluates to 0 because the operand(3 == 5)is 0 (false).(3 == 5) && (3 > 5)evaluates to 0 because both operands(3 == 5)and(3 > 5)are 0 (false).(3 != 5) || (3 < 5)evaluates to 1 because both operands(3 != 5)and(3 < 5)are 1 (true).(3 != 5) || (3 > 5)evaluates to 1 because the operand(3 != 5)is 1 (true).(3 == 5) || (3 > 5)evaluates to 0 because both operands(3 == 5)and(3 > 5)are 0 (false).!(5 == 2)evaluates to 1 because the operand(5 == 2)is 0 (false).!(5 == 5)evaluates to 0 because the operand(5 == 5)is 1 (true).
5. C++ Bitwise Operators
In C++, bitwise operators are used to perform operations on individual bits. They can only be used alongside char and int data types.
| Operator | Description |
|---|---|
& | Binary AND |
| | Binary OR |
^ | Binary XOR |
~ | Binary One's Complement |
<< | Binary Shift Left |
>> | Binary Shift Right |