I have never seen the usecase for pre-increment and post-increment in actual code. The only place i see them most often are puzzles.
My opinion is, it introduces more confusion rather than being useful.
- is there any real use case scenario for this
-
can’t this can be done by using +=
y = x++y = x
x += 1
It’s just a shorter way of writing the same thing and it’s only confusing to those who don’t deeply understand C (a). The same argument could be made for replacing:
with:
since any
forcan also be done withwhile, or:since any loop construct can be built out of conditions and
goto. But (I’m hoping) you wouldn’t do that, would you?(a) I sometimes like to explain this to my students as simple statements and side effects, something that allows C code to be more succinct with usually no or minimal loss in readability.
For the statement:
the statement is assigning
xtoywith the side effect thatxis incremented afterwards.++xis the same, it’s just that the side effect happens beforehand.Similarly, the side effect of an assignment is that it evaluates as the value assigned, meaning you can do things like:
and which makes things like:
perfectly valid, but useless, C statements.