Operators are symbols that perform operations on values (called operands). Think of them like math symbols (+, -, ×, ÷) or comparison words (greater than, equal to).
In programming, operators help:
- Manipulate data (numbers, text, etc.).
- Control how a program works (decision-making, loops).
C++ provides many operators with the ability to perform different tasks. Understanding them helps you write better and faster code.
Types of Operators in C++
The following are different Operators in C++.
1. Arithmetic Operators
Arithmetic Operators perform mathematical calculations. Here are types of arithmetic operators:
a. Binary Arithmetic Operators (Two Operands)
These work with two numbers:
Operator | Meaning | Example |
---|---|---|
+ | Addition | 5 + 3 → 8 |
- | Subtraction | 5 - 3 → 2 |
* | Multiplication | 5 * 3 → 15 |
/ | Division | 10 / 3 → 3 (integer) or 3.333 (float) |
% | Modulo (remainder) | 10 % 3 → 1 |
Note:
- Integer division (
int / int
) Discards the decimal part. - Use
float
ordouble
For exact division.
b. Unary Arithmetic Operators (One Operand)
These work with a single number:
Operator | Meaning | Example |
---|---|---|
++ | Increment (add 1) | x++ (postfix), ++x (prefix) |
-- | Decrement (subtract 1) | x-- (postfix), --x (prefix) |
+ | Unary plus (no change) | +5 → 5 |
- | Unary minus (negation) | -5 → -5 |
Difference Between Prefix & Postfix:
++x
→ First increments, then uses the value.x++
→ First uses the value, then increments.
Example:
int a = 5;
cout << a++; // Prints 5, then a becomes 6
cout << ++a; // First a becomes 7, then prints 7
2. Assignment Operators
Assignment operators assign values to variables.
a. Simple Assignment (=
)
A simple assignment stores a value in a variable.
int x = 10; // x now holds 10
b. Compound Assignment (Shortcuts)
Combine arithmetic and assignment:
Operator | Meaning | Example | Equivalent |
---|---|---|---|
+= | Add & assign | x += 5 | x = x + 5 |
-= | Subtract & assign | x -= 3 | x = x - 3 |
*= | Multiply & assign | x *= 2 | x = x * 2 |
/= | Divide & assign | x /= 2 | x = x / 2 |
%= | Modulo & assign | x %= 3 | x = x % 3 |
Why Use Them?
- Shorter code.
- Faster to write.
3. Comparison (Relational) Operators: Making Decisions
Compare two values and return true
or false
.
Operator | Meaning | Example |
---|---|---|
== | Equal to | 5 == 5 → true |
!= | Not equal | 5 != 3 → true |
> | Greater than | 5 > 3 → true |
< | Less than | 5 < 3 → false |
>= | Greater than or equal | 5 >= 5 → true |
<= | Less than or equal | 5 <= 3 → false |
Used in:
if
conditions.- Loops (
while
,for
).
Common Mistake:
- Using
=
(assignment) instead of==
(comparison).
4. Logical Operators: Combining Conditions
Combine multiple conditions:
Operator | Meaning | Example |
---|---|---|
&& | AND (both must be true) | (5 > 3) && (2 < 4) → true |
! | NOT (reverse condition) | !(5 == 3) → true |
Short-Circuiting:
&&
stops if the first condition isfalse
.||
stops if the first condition istrue
.
5. Bitwise Operators (Advanced – Optional)
Work on binary bits (0s and 1s).
Operator | Meaning | Example |
---|---|---|
& | Bitwise AND | 5 & 3 → 1 |
^ | Bitwise XOR | 5 ^ 3 → 6 |
~ | Bitwise NOT | ~5 → -6 |
<< | Left shift | 5 << 1 → 10 |
>> | Right shift | 5 >> 1 → 2 |
Uses:
- Low-level programming.
- Optimizing code.
Other Important Operators
Operator | Meaning | Example |
---|---|---|
?: | Ternary (short if-else ) | (x > 5) ? "Yes" : "No" |
, | Comma (separates expressions) | x = (a=5, a+1) → x=6 |
:: | Scope resolution | std::cout |
. and -> | Member access | obj.x or ptr->x |
sizeof | Size of variable | sizeof(int) → 4 (bytes) |
Operator Precedence and Associativity
Some operators run before others (like math: *
before +
).
Example:
int x = 5 + 3 * 2; // 3*2=6 → 5+6=11
Common Precedence Rules
- Parentheses
()
- Unary (
++
,--
,!
,~
) - Arithmetic (
*
,/
,%
→+
,-
) - Comparison (
<
,>
,<=
,>=
) - Equality (
==
,!=
) - Logical (
&&
→||
) - Assignment (
=
,+=
,-=
, etc.)
Associativity
- Left-to-right:
a + b + c
→(a + b) + c
- Right-to-left:
x = y = 5
→y=5
thenx=y
Using Parentheses for Clarity
Always use ()
to avoid confusion:
int result = (5 + 3) * 2; // 8 * 2 = 16
Practical Examples
The following are practical examples of operators in C++ that clear all your confusion.
1. Simple Calculations
int a = 10, b = 3;
cout << a + b; // 13
cout << a % b; // 1
2. Checking User Age
int age;
cin >> age;
if (age >= 18) {
cout << "Adult";
} else {
cout << "Minor";
}
3. Looping with Operators
for (int i = 0; i < 10; i++) {
cout << i << " ";
}
Common Mistakes
if (x = 5)
→ Wrong! (Use==
).5 / 2
→2
(Use5.0 / 2
for2.5
).