Menu
Coddy logo textTech

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):

  1. The operator goes BEFORE the variable
  2. The value changes IMMEDIATELY
  3. 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--):

  1. The operator goes AFTER the variable
  2. The original value is used first
  3. 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 6

pre-increment:

int score = 5;

int result2 = ++score;
// res is 6
// score is 6

Cheat 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 6

Post-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 6

Try it yourself

This lesson doesn't include a code challenge.

quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Fundamentals