wilbomc
wilbomc

Reputation: 183

C# ++ unary operator increment issue

Question regarding the C# ++ (right placed) operator.

As the left placed ++ operator, for example, ++var, (say holding an int value of 1) will increment by 1 before any other computation takes place (example value of 1 will become 2 after expression is executed and result displayed).

Can anyone explain the difference between the left placed operator and the right placed operator? (var++) as it does not seem to increment even after the expression is executed as it is supposed to do. Here is some sample code:

        int var1, var2 = 5, var3 = 6;

        var1 = var2++ * --var3;
        Console.WriteLine(" {0} ", var1);
        Console.ReadKey();

This is just 5 x 5 due to the decrement of var3 but without the decrement it is 5 x 6 and var2++ appears to have no effect other than the value of 5 it carries. If anyone can shed light on this topic I would be grateful. Thanks.

***Issue solved. A lot of great answers and input guys, was hard trying to decide what answer to accept but you are all winners here! Thanks again for the help! =)

Upvotes: 2

Views: 712

Answers (5)

maliks
maliks

Reputation: 1112

Here is some sample class leveraging concept of PreFix and PostFix increment operators. The code is written with comments making the output more clearer.

public class Test
    {
            public Test()
            { }


        public static void Main(string[] args)
        {
            int i = 0;

            Console.WriteLine("\n" + "Displaying Initial            i      =     " + i + "\n");   // Prints 0 i.e. Initial value of i

            Console.WriteLine("\n" + "Displaying PostFix            i++    =     " + i++ + "\n"); // Prints 0. Then value of i becomes 1.

            Console.WriteLine("\n" + "Displaying Post-incremented   i      =     " + i + "\n");   // Prints 1 i.e. Value of i after Post-increment

            Console.WriteLine("\n" + "Displaying PreFix             ++i    =     " + ++i + "\n"); // Prints 2. Then value of i incremented to 2

            Console.WriteLine("\n" + "Displaying Pre-incremented    i      =     " + i + "\n");   // Prints 2 i.e. Value of i after Pre-increment

            Console.WriteLine("\n" + "---------------------------------------------" + "\n");

            int j = 0;

            Console.WriteLine("\n" + "Displaying Initial            j      =     " + j + "\n");   // Prints 0 i.e. Initial value of j

            Console.WriteLine("\n" + "Displaying PreFix             ++j    =     " + ++j + "\n"); // Prints 1. Then value of j incremented to 1.

            Console.WriteLine("\n" + "Displaying Pre-incremented    j      =     " + j + "\n");   // Prints 1 i.e. Value of j after Pre-increment

            Console.WriteLine("\n" + "Displaying PostFix            j++    =     " + j++ + "\n"); // Prints 1. Then value of j incremented to 2.

            Console.WriteLine("\n" + "Displaying Post-incremented   j      =     " + j + "\n");   // Prints 2 i.e. Value of j after Post-increment

            Console.ReadLine();
        }
    }

Upvotes: 0

Marc Gravell
Marc Gravell

Reputation: 1064324

The var2 value is incremented after evaluation for the multiplication; the var3 value is decremented before evaluation for the multiplication; it can be roughly conceptualised as:

    var tmp = var2; // 5
    var2 = var2 + 1; // 5 + 1 = 6
    var3 = var3 - 1; // 6 - 1 = 5;
    var1 = tmp * var3; // 5 * 5 = 25;

So the 25 is correct. If you inspect var2 you will find that it has incremented as expected.

However! If a complex calculation involving ++foo and bar-- gets confusing, then simply don't do it inline; break the computation down into simpler pieces, and execute that. The compiler won't judge you.

Upvotes: 1

Botz3000
Botz3000

Reputation: 39670

++x (prefix increment) increments the value before the expression is evaluated. Thus it first increments the value and then returns it.

x++ (postfix increment) increments the value after the expression is evaluated. Thus, it returns the unchanged value, and only after that, x is incremented.

After the line var1 = var2++ * --var3;, var2 is actually 6, because it was incremented after its value was evaluated.

So your code:

var1 = var2++ * --var3;

does something like this:

int oldVar2 = var2;
var2 = var2 + 1;
var3 = var3 - 1;
var1 = oldVar2 * var3;

Upvotes: 4

Tigran
Tigran

Reputation: 62265

In C# the only difference between left-placed operator and right-placed operator is the actual returned value after computation.

In case of left-hand operator, returned new, or incremented value.

In case of right-hand operator, returned value is the "old" one, even if the real value was incremented.

But important to know that in both cases the sequence of operations executed is exactly the same.

Upvotes: 2

Henrik
Henrik

Reputation: 23324

        int var1, var2 = 5, var3 = 6;

        var1 = var2++ * --var3;
        Console.WriteLine(" {0} ", var1);
        Console.WriteLine(" {0} ", var2);
        Console.ReadKey();

Output:

25

6

So var2 is incremented as expected.

Upvotes: 5

Related Questions