Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a post increment by more than one?

Tags:

java

c#

math

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?

like image 234
Ethan Brouwer Avatar asked May 04 '15 22:05

Ethan Brouwer


2 Answers

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.

like image 190
O. Jones Avatar answered Sep 20 '22 03:09

O. Jones


No. a += 5 isn't a post increment. It's an increment.

a++ is post-increment. And ++a is pre-increment.

like image 28
Zer0 Avatar answered Sep 19 '22 03:09

Zer0