Post Increment/Decrement
Part of the Fundamentals section of Coddy's C++ journey — lesson 19 of 74.
In the previous lesson, we covered the increment (++) and decrement (--) operators. These operators have two forms: prefix and postfix.
Knowing which form to use matters in real programs — for example, when tracking a game score, counting loop iterations, or advancing through a list of items.
The prefix form is written before the variable (e.g., ++x or --x), and the postfix form is written after the variable (e.g., x++ or x--).
The difference between the two forms is subtle but important:
- Prefix form: Increments/decrements the variable and then returns the new value.
- Postfix form: Returns the current value of the variable and then increments/decrements it.
Here's an example to illustrate the difference:
int x = 5;
int y = x++;
// y = 5, x = 6 (postfix)
int a = 5;
int b = ++a;
// b = 6, a = 6 (prefix)In the first case, y is assigned the original value of x (5), and then x is incremented to 6. In the second case, a is incremented first, and then its new value (6) is assigned to b.
The same logic applies to the decrement operator:
int x = 5;
int y = x--;
// y = 5, x = 4 (postfix)
int a = 5;
int b = --a;
// b = 4, a = 4 (prefix)Challenge
BeginnerYou are given a code with initializations of x, y, and z. (Don't delete these lines!)
Your task is to use the increment/decrement operators to perform the following operations, in this order:
- Use the postfix increment operator to assign the current value of
xtoaand then incrementx. - Use the prefix decrement operator to decrement
yand assign its new value tob. - Use the postfix decrement operator to assign the current value of
ztocand then decrementz.
After performing these operations, print the values of a, b, c, x, y, and z to the console in the following format:
a: [value of a]
b: [value of b]
c: [value of c]
x: [value of x]
y: [value of y]
z: [value of z]Cheat sheet
Increment (++) and decrement (--) operators have two forms:
- Prefix form (
++x,--x): Increments/decrements the variable first, then returns the new value - Postfix form (
x++,x--): Returns the current value first, then increments/decrements the variable
Examples:
int x = 5;
int y = x++; // y = 5, x = 6 (postfix)
int a = 5;
int b = ++a; // b = 6, a = 6 (prefix)
int x = 5;
int y = x--; // y = 5, x = 4 (postfix)
int a = 5;
int b = --a; // b = 4, a = 4 (prefix)Try it yourself
#include <iostream>
int main() {
int x = 10;
int y = 20;
int z = 30;
int a, b, c;
// Type your code below
// Don't change the lines below
std::cout << "a: " << a << std::endl;
std::cout << "b: " << b << std::endl;
std::cout << "c: " << c << std::endl;
std::cout << "x: " << x << std::endl;
std::cout << "y: " << y << std::endl;
std::cout << "z: " << z << std::endl;
return 0;
}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 Comparison3Variables Part 2
Type DeclarationNaming ConventionsRecap - Initialize VariablesType Casting Part 1Type Casting Part 26Decision Making
If StatementIf - ElseSwitch StatementConditional OperatorRecap - If ElseNested If - Else