简体   繁体   中英

Is there a post increment by more than one?

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 .

The following prints the required results but is not exactly beautiful code:

int a = 5;
System.out.println(a+=5);
System.out.println((a+=5)-5);
System.out.println(a);

Prints:

10, 10, 15

a+=5 returns the value of a after the increment. (a+=5)-5 increments a and returns its value before the increment.

a=+5 just compiles to a=5 . This performs assignment and the unary plus operator . It is akin to doing a=-5 ( a equals negative 5).

System.out.println(+5);

Prints:

5

In C# the equivalent code generates the same output:

int a = 5;
Console.WriteLine(a+=5);
Console.WriteLine((a+=5)-5);
Console.WriteLine(a);
Console.WriteLine(+5);

Prints:

10, 10, 15, 5

There is no such operator in any language that I know of, but you can write your own C# function to do the same thing!

static void postAdd<T>(ref T lhs, T rhs) {
    T saved = lhs;
    lhs += rhs;
    return saved;
}

This is not possible in Java because Java does not support pass-by-reference with ref .

Console.WriteLine(a+=5);

Is perfectly legal in the langauge. It is a sort of pre-increment operator in that it will increment the variable a before returning the value to the WriteLine call.

=+ isn't a valid operator since it isn't defined by the C# standard. All of the C# operators can be found on this page: https://msdn.microsoft.com/en-us/library/ewkkxkwb.aspx

Its not possible to create your own operators, because the compiler would have to know how to make expression trees or parse the line to generate the IL. The aside to this is that, by a lot of work and especially with the release of Rosyln, you could in theory make your own language (like IronPython) that had those operators.

The closest that you can come to your own operator are things like extension methods, for example:

public class Extensions
{
    public static int AddMul(this int x, int add, int mull)
    {
        return x * mull + add;
    }
}

which would be used like:

int x = 4;
int y = x.AddMul(2, 3);  //y now equals 14

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM