Operators in C++
Operators are special symbols that perform operations on values and variables. Think of operators as the verbs of a programming language — they do things. C++ has a rich set of operators grouped into several categories.
1. Arithmetic Operators
These operators perform basic mathematical operations:
| Operator | Name | Example | Result |
|---|---|---|---|
+ | Addition | 5 + 3 | 8 |
- | Subtraction | 9 - 4 | 5 |
* | Multiplication | 3 * 4 | 12 |
/ | Division | 10 / 2 | 5 |
% | Modulus (remainder) | 10 % 3 | 1 |
#include <iostream>
using namespace std;
int main() {
int a = 10, b = 3;
cout << "Add: " << a + b << endl;
cout << "Subtract: " << a - b << endl;
cout << "Multiply: " << a * b << endl;
cout << "Divide: " << a / b << endl;
cout << "Modulus: " << a % b << endl;
return 0;
}
Output:
Add: 13
Subtract: 7
Multiply: 30
Divide: 3
Modulus: 12. Relational (Comparison) Operators
These compare two values and return true (1) or false (0):
| Operator | Meaning | Example |
|---|---|---|
== | Equal to | 5 == 5 → true |
!= | Not equal to | 5 != 3 → true |
> | Greater than | 7 > 3 → true |
< | Less than | 2 < 9 → true |
>= | Greater than or equal | 5 >= 5 → true |
<= | Less than or equal | 4 <= 6 → true |
int x = 10, y = 20;
cout << (x == y) << endl; // 0 (false)
cout << (x < y) << endl; // 1 (true)
3. Logical Operators
Logical operators combine two or more conditions:
| Operator | Name | Description |
|---|---|---|
&& | AND | True only if both conditions are true |
|| | OR | True if at least one condition is true |
! | NOT | Reverses the condition (true becomes false) |
int age = 20;
bool hasID = true;
if (age >= 18 && hasID) {
cout << "Entry allowed" << endl;
}
Output:
Entry allowed4. Assignment Operators
Assignment operators store values in variables. The basic one is =, but there are shorthand versions:
| Operator | Example | Equivalent To |
|---|---|---|
= | x = 5 | Assign 5 to x |
+= | x += 3 | x = x + 3 |
-= | x -= 2 | x = x - 2 |
*= | x *= 4 | x = x * 4 |
/= | x /= 2 | x = x / 2 |
%= | x %= 3 | x = x % 3 |
int score = 100;
score += 10;
cout << score; // 110
5. Increment and Decrement Operators
These operators increase or decrease a value by 1:
++— Increment (add 1)--— Decrement (subtract 1)
They can appear before (prefix) or after (postfix) the variable:
int a = 5;
cout << a++ << endl; // Prints 5, then a becomes 6
cout << ++a << endl; // a becomes 7, then prints 7
Output:
5
76. Bitwise Operators
These operate on individual bits of integer values:
| Operator | Name | Example |
|---|---|---|
& | Bitwise AND | 5 & 3 = 1 |
| | Bitwise OR | 5 | 3 = 7 |
^ | Bitwise XOR | 5 ^ 3 = 6 |
~ | Bitwise NOT | ~5 = -6 |
<< | Left shift | 5 << 1 = 10 |
>> | Right shift | 5 >> 1 = 2 |
7. Ternary Operator (Conditional Operator)
The ternary operator is a shorthand for simple if-else conditions:
condition ? value_if_true : value_if_false
int a = 10, b = 20;
int max = (a > b) ? a : b;
cout << "Maximum: " << max << endl;
Output:
Maximum: 20Operator Precedence
When multiple operators appear in one expression, C++ follows a precedence order (similar to BODMAS in math). Higher precedence operators are evaluated first.
int result = 10 + 3 * 2; // 3*2 = 6, then 10+6 = 16
cout << result; // 16
Use parentheses to control evaluation order:
int result = (10 + 3) * 2; // 13 * 2 = 26
cout << result; // 26
Key Takeaways
- Arithmetic operators perform math operations.
- Relational operators compare values and return true/false.
- Logical operators combine conditions using AND, OR, NOT.
- Assignment operators store or update values in variables.
- The ternary operator is a concise way to write simple if-else logic.
- Operator precedence determines the order of evaluation in expressions.
