Post Increment/Decrement
Part of the Fundamentals section of Coddy's C# journey — lesson 18 of 69.
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 6Post-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 6Another example
post-increment:
int score = 5;
int res1 = score++;
// res1 is 5
// score is 6pre-increment:
int score = 5;
int result2 = ++score;
// res 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 Shortcuts5Operators Part 2
Comparison OperatorsLogical Operators Part 1Logical Operators Part 2Recap - Simple LogicLogical Operators Part 3