Post Increment/Decrement
Part of the Fundamentals section of Coddy's Java journey — lesson 18 of 73.
Increment (++) and Decrement (--) operators can be used in two ways:
Pre-increment/decrement (++x or --x):
- The operator goes BEFORE the variable
- The value changes IMMEDIATELY
The new value is used in the expression
int x = 5; int y = ++x; // x is increased to 6 first, then y becomes 6
Post-increment/decrement (x++ or x--):
- The operator goes AFTER the variable
- The original value is used first
The value changes AFTER the expression
int x = 5; int y = x++; // y becomes 5 first, then x increases to 6
Another example
post-increment:
int score = 5;
int res1 = score++;
// res1 is 5
// score is 6pre-increment:
int score = 5;
int result2 = ++score;
// result2 is 6
// score is 6Cheat sheet
Increment (++) and Decrement (--) operators can be used in two ways:
Pre-increment/decrement (++x or --x):
- Operator goes BEFORE the variable
- Value changes IMMEDIATELY
- New value is used in the expression
int x = 5;
int y = ++x;
// x is increased to 6 first, then y becomes 6Post-increment/decrement (x++ or x--):
- Operator goes AFTER the variable
- Original value is used first
- Value changes AFTER the expression
int x = 5;
int y = x++;
// y becomes 5 first, then x increases to 6Try it yourself
This lesson doesn't include a code challenge.
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Fundamentals
4Operators Part 1
Arithmetic OperatorsModulo OperatorIncrement/DecrementPost Increment/DecrementArithmetic ShortcutsComparison OperatorsString Comparison5Operators Part 2
Logical Operators Part 1Logical Operators Part 2Recap - Simple LogicLogical Operators Part 3Logical Operators Part 43Variables Part 2
ConstantsNaming ConventionsRecap - Initialize VariablesType Casting Part 1Type Casting Part 26Decision Making
If StatementIf - ElseSwitch StatementTernary OperatorRecap - If ElseNested If - Else