[nem-en] Increment and decrement operators

Kannan Goundan cakoose at yahoo.com
Mon Feb 27 03:09:12 CET 2006


Why are there both "x++" and "++x" if the expression doesn't return a
value?  Is it just to avoid syntax errors for those users who use one
or the other out of habit?

Also, I couldn't figure out exactly how the operator behaves.  It
seems the commas in a function call are sequence points but the part
of an infix expression aren't, which gives weird results:

class Increment
{
   static Main() : void
   {
     mutable x : uint = 1;

     x = 1;
     System.Console.Write("Val: {0}", {x++; x});
     System.Console.WriteLine(", End: {0}", x);

     x = 1;
     System.Console.Write("Val: {0}", {++x; x});
     System.Console.WriteLine(", End: {0}", x);

     x = 1;
     System.Console.Write("Add: {0}", {x++; x} + {x++; x});
     System.Console.WriteLine(", End: {0}", x);

     x = 1;
     System.Console.Write("Add: {0}", {++x; x} + {++x; x});
     System.Console.WriteLine(", End: {0}", x);

     x = 1;
     System.Console.Write("Add: {0}", Add({x++; x}, {x++; x}));
     System.Console.WriteLine(", End: {0}", x);

     x = 1;
     System.Console.Write("Add: {0}", Add({++x; x}, {++x; x}));
     System.Console.WriteLine(", End: {0}", x);
   }

   static Add(a : uint, b : uint) : uint
   {
     a + b;
   }
}

-------------------
Output:

Val: 2, End: 2
Val: 2, End: 2
Add: 2, End: 3
Add: 2, End: 3
Add: 5, End: 3
Add: 5, End: 3


__________________________________________________
Do You Yahoo!?
Tired of spam?  Yahoo! Mail has the best spam protection around 
http://mail.yahoo.com 



More information about the devel-en mailing list