


Khusboo Tayal
Operators are symbols that perform specific operations on one or more operands (values or variables). These operations can include arithmetic calculations, logical comparisons, bit manipulations, and more.
int a = 5; int b = 3; int sum = a + b; // '+' is an arithmetic operator
C++ provides a wide range of operators categorized into several types:
These operators perform basic mathematical operations.
| Operator | Description | Example | Result |
|---|---|---|---|
| + | Addition | a + b | Sum |
| - | Subtraction | a - b | Difference |
| * | Multiplication | a * b | Product |
| / | Division | a / b | Quotient |
| % | Modulus (Remainder) | a % b | Remainder |
#include <iostream>
using namespace std;
int main() {
int a = 10, b = 3;
cout << "Addition: " << a + b << endl;
cout << "Subtraction: " << a - b << endl;
cout << "Multiplication: " << a * b << endl;
cout << "Division: " << a / b << endl;
cout << "Modulus: " << a % b << endl;
return 0;
}Addition: 13 Subtraction: 7 Multiplication: 30 Division: 3 Modulus: 1
These operators compare two values and return a boolean value (true or false).
#include <iostream>
using namespace std;
int main() {
int a = 10, b = 5;
cout << "a == b: " << (a == b) << endl;
cout << "a != b: " << (a != b) << endl;
cout << "a > b: " << (a > b) << endl;
cout << "a < b: " << (a < b) << endl;
cout << "a >= b: " << (a >= b) << endl;
cout << "a <= b: " << (a <= b) << endl;
return 0;
}a == b: 0 a != b: 1 a > b: 1 a < b: 0 a >= b: 1 a <= b: 0
These operators are used to combine or negate conditions.
| Operator | Description | Example |
|---|---|---|
| && | Logical AND | (a > b) && (b > 0) |
| ! | Logical NOT | !(a > b) |
| || | Logical OR | (a > b) || (b > 0) |
#include <iostream>
using namespace std;
int main() {
bool isSunny = true;
bool isWeekend = false;
cout << "AND: " << (isSunny && isWeekend) << endl;
cout << "OR: " << (isSunny || isWeekend) << endl;
cout << "NOT: " << (!isSunny) << endl;
return 0;
}AND: 0 OR: 1 NOT: 0
These operators perform operations on individual bits of data.
| Operator | Description | Example |
|---|---|---|
| & | Bitwise AND | a & b |
| | | | | Bitwise OR |
| ^ | Bitwise XOR | a ^ b |
| ~ | Bitwise NOT | ~a |
| << | Left Shift | a << 2 |
| >> | Right Shift | a >> 2 |
#include <iostream>
using namespace std;
int main() {
int a = 5, b = 3; // a = 0101, b = 0011
cout << "Bitwise AND: " << (a & b) << endl;
cout << "Bitwise OR: " << (a | b) << endl;
cout << "Bitwise XOR: " << (a ^ b) << endl;
cout << "Bitwise NOT: " << (~a) << endl;
cout << "Left Shift: " << (a << 1) << endl;
cout << "Right Shift: " << (a >> 1) << endl;
return 0;
}Bitwise AND: 1 Bitwise OR: 7 Bitwise XOR: 6 Bitwise NOT: -6 Left Shift: 10 Right Shift: 2
These operators assign values to variables.
| Operator | Description | Example |
|---|---|---|
| = | Assign | a = 10 |
| += | Add and assign | a += 5 |
| -= | Subtract and assign | a -= 5 |
| *= | Multiply and assign | a *= 2 |
| /= | Divide and assign | a /= 2 |
| %= | Modulus and assign | a %= 2 |
#include <iostream>
using namespace std;
int main() {
int x = 5;
cout << "Post-Increment: " << x++ << endl;
cout << "After Post-Increment: " << x << endl;
cout << "Pre-Increment: " << ++x << endl;
return 0;
}Post-Increment: 5 After Post-Increment: 6 Pre-Increment: 7
A compact way to write an if-else statement.
condition ? expression1 : expression2;
int max = (a > b) ? a : b;
In this tutorial, you learned: