Junior — Middle
What is the difference between prefix and postfix increment operations in programming languages?
sobes.tech AI
Answer from AI
Prefix increment (++i) increases the value of the variable by 1 and returns the incremented value.
Postfix increment (i++) first returns the current value of the variable, then increases it by 1.
Example:
int i = 5;
int a = ++i; // i becomes 6, a = 6
int b = i++; // b = 6, i becomes 7
This is important to consider when used in expressions where the result of the increment is used immediately.