PRO
Archana
asked

Expert
Abhay Jajodia answered
Hi Archana,
We use increment (++) and decrement (--) operators in C++ to quickly increase or decrease a variable's value by 1. They're mainly used for:
Simpler code
Instead of writingi = i + 1, you can just writei++. It’s shorter and easier to read.Looping
These operators are commonly used in loops. For example:for (int i = 0; i < 5; i++) { cout << i << " "; }This loop prints:
0 1 2 3 4Prefix vs Postfix
++i(prefix) increases the value before it’s usedi++(postfix) uses the value first, then increases it
Example:
int i = 5; cout << ++i; // Outputs 6 cout << i++; // Outputs 6, then i becomes 7
Both forms are useful depending on when you want the increment or decrement to happen.
If you have more questions, I’m here to help.
C++
This question was asked as part of the Learn C++ Basics course.
Our Experts
Sudip BhandariHead of Growth/Marketing
Apekchhya ShresthaSenior Product Manager
Kelish RaiTechnical Content Writer
Abhilekh GautamSystem Engineer
Palistha SinghTechnical Content Writer
Sarthak BaralSenior Content Editor
Saujanya Poudel
Abhay Jajodia
Nisha SharmaTechnical Content Writer
Udayan ShakyaTechnical Content Writer
