Increment and decrement operators help you quickly add or subtract 1 from a number. They make your code shorter and easier to read.
Increment and decrement operators in C++
++variable; // adds 1 to variable --variable; // subtracts 1 from variable variable++; // adds 1 to variable after current use variable--; // subtracts 1 from variable after current use
There are two types: prefix (++variable) and postfix (variable++).
Prefix changes the value before using it, postfix changes it after.
int x = 5; ++x; // x becomes 6
int y = 5; y++; // y becomes 6 after this line
int a = 3; --a; // a becomes 2
int b = 3; b--; // b becomes 2 after this line
This program shows how prefix and postfix increments and decrements change the variable and when the change happens relative to printing.
#include <iostream> int main() { int count = 0; std::cout << "Initial count: " << count << "\n"; std::cout << "Using prefix increment: " << ++count << "\n"; // count becomes 1, then prints 1 std::cout << "Using postfix increment: " << count++ << "\n"; // prints 1, then count becomes 2 std::cout << "Count after postfix increment: " << count << "\n"; // prints 2 std::cout << "Using prefix decrement: " << --count << "\n"; // count becomes 1, then prints 1 std::cout << "Using postfix decrement: " << count-- << "\n"; // prints 1, then count becomes 0 std::cout << "Count after postfix decrement: " << count << "\n"; // prints 0 return 0; }
Use prefix (++x) when you want the updated value immediately.
Use postfix (x++) when you want to use the original value first, then update.
Be careful when using these operators inside complex expressions to avoid confusion.
Increment (++) adds 1; decrement (--) subtracts 1.
Prefix form changes value before use; postfix changes after use.
They make counting and updating numbers simple and clean.
Practice
++ operator do in C++?Solution
Step 1: Understand the increment operator
The++operator increases the value of a variable by 1.Step 2: Compare with other options
Other options describe different operations like subtraction or multiplication, which are not what++does.Final Answer:
Adds 1 to the variable -> Option CQuick Check:
Increment (++) means add 1 [OK]
- Confusing ++ with --
- Thinking ++ multiplies or divides
- Mixing prefix and postfix effects
x by 1 using the decrement operator?Solution
Step 1: Identify prefix decrement syntax
The prefix decrement operator is--x;, which decrements the value first.Step 2: Check other options
x--;is postfix.x-1;subtracts but does not modifyx.x--1;is invalid.Final Answer:
--x; -> Option AQuick Check:
Prefix decrement is -- before variable [OK]
- Writing x--1 which is invalid
- Using x-1; which does not change x
- Confusing decrement with subtraction
int x = 5;
int y = x++ + ++x;
std::cout << y;
Solution
Step 1: Evaluate
Initially,x++and++xvaluesx = 5.x++uses 5 then increments to 6. Then++xincrements 6 to 7 and uses 7.Step 2: Calculate the sum
Sum is 5 (fromx++) + 7 (from++x) = 12.Step 3: Confirm final value of y
So,y = 12.Final Answer:
12 -> Option BQuick Check:
Postfix uses old value, prefix increments first [OK]
- Adding incremented values twice
- Mixing prefix and postfix effects
- Ignoring order of operations
int a = 3;
int b = ++a + a--;
std::cout << b;
Solution
Step 1: Analyze variable modifications
The variableais modified twice in the same expression: once by++aand once bya--.Step 2: Understand sequence point rules
In C++, modifying a variable more than once between sequence points causes undefined behavior.Final Answer:
Undefined behavior due to modifying variable twice without sequence point -> Option AQuick Check:
Multiple ++/-- on same var in one expression causes undefined behavior [OK]
- Assuming output is always predictable
- Ignoring undefined behavior rules
- Thinking syntax error occurs here
sum?int sum = 0;
int i = 0;
while (i++ < 5) {
sum += i;
}
Solution
Step 1: Trace the loop iterations and values of i
Initially,i = 0. Conditioni++ < 5usesithen increments. Iterations:
1) i=0 (check 0<5 true), then i=1, sum+=1
2) i=1 (check 1<5 true), then i=2, sum+=2
3) i=2 (check 2<5 true), then i=3, sum+=3
4) i=3 (check 3<5 true), then i=4, sum+=4
5) i=4 (check 4<5 true), then i=5, sum+=5
6) i=5 (check 5<5 false), loop endsStep 2: Calculate sum
Sum = 1+2+3+4+5 = 15Final Answer:
15 -> Option DQuick Check:
Postfix increments after comparison; sum 1 to 5 [OK]
- Assuming i increments before comparison
- Adding wrong range of numbers
- Confusing prefix and postfix in loop condition
Ready to prove it?
Pass the test and this pattern moves unknown
