Just had an interesting thought. In languages like C# and Java, I know that when it comes to incrementing and decrementing, you can do a post, or pre-increment/decrement. Like this:
int a = 5;
Console.WriteLine(a++); // Would print out 5,
// And then increment a to 6
Console.WriteLine(++a); // Would then print out 7 because
// it increments a before printing it out
But, I was wondering if there is any such thing where one might do something like this:
int a = 5;
Console.WriteLine(a += 5); // Would print out 5,
// And then increment a to 10
Console.WriteLine(a =+ 5); // (Or something along those lines)
// To print out 15 at this point
Just interested and didn't really know where or how to look for the answer, so wondered if anyone on SO would know anything more about it. Thanks!
Edit: Added my question from the comments
Where exactly are a += 5
and a =+
5 defined? I've never seen the second in use. Does it exist at all...? Do they compile to the same thing?
In the old days, the C language offered this syntax as a shortcut for adding or subtracting a value from a variable.
a =+ 5;
b =- 5;
But early on in the life of C, dmr (of blessed memory) and ken deprecated that syntax in favor of
a += 5;
b -= 5;
for precisely the same purpose, because it's far too easy to write b=-5
which means something entirely different from b -= 5
. This "experienced" programmer remembers rewriting a bunch of code to match the new language spec.
So there has never been pre- or post- increment semantics in those constructions like there is in a++
or --b
.
No. a += 5
isn't a post increment. It's an increment.
a++
is post-increment. And ++a
is pre-increment.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With