2

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! =)

jam
  • 3,640
  • 5
  • 34
  • 50
wilbomc
  • 183
  • 3
  • 13

5 Answers5

5
        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.

Henrik
  • 23,186
  • 6
  • 42
  • 92
4

++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;
Botz3000
  • 39,020
  • 8
  • 103
  • 127
  • 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! =) – wilbomc Jul 17 '12 at 19:09
2

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.

Tigran
  • 61,654
  • 8
  • 86
  • 123
1

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.

Henrik
  • 23,186
  • 6
  • 42
  • 92
Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900
  • "The var2 value is incremented after evaluation for the multiplication". This statement is incorrect even according to your sample. – Polity Jul 17 '12 at 08:01
  • @Polity no, the "evaluation for the multiplication" is where it stores the value in `tmp`. Note that it does this **before** it increments the value of `var2`. In reality, it would sit as a free value on the stack, not in a designated local, but: same effect. – Marc Gravell Jul 17 '12 at 11:59
0

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();
        }
    }
maliks
  • 1,102
  • 3
  • 18
  • 42