Increment & Decrement Operators in Java
π What Are They?
These operators are used to increase or decrease a variable's value by 1.
πΉ Increment Operator (++)
- Increases the value by 1
πΈ Decrement Operator (--)
-
Decreases the value by 1
β Two Forms: Pre and Post
| Type | Example | What it does |
|---|---|---|
| Pre-increment | ++x |
Increments first, then uses the value |
| Post-increment | x++ |
Uses the value first, then increments it |
| Pre-decrement | --x |
Decrements first, then uses the value |
| Post-decrement | x-- |
Uses the value first, then decrements it |
π Example:
β οΈ Common Use-Cases:
- Loop counters (
for,while) - Simple arithmetic
- Conditional updates
π§ Tip:
- Use pre (
++x) when you need the updated value immediately. - Use post (
x++) when the update can wait until after the current use.
Β
Β
