Home > ASP.NET, C#, Interview Questions, Programming, Teasers, Tips & Tricks > C# Increment operator (++) question

C# Increment operator (++) question

Well, this is a nice one. We all familiar with incremental operators, but from what i saw yesterday… well, there is no need to give a developer some tricky assignment so he could find a way to make some bugs.

3bj4h786fc


What will be the output of the next code ?

static void Main(string[] args)
{
    int i = 100;
    for (int n = 0; n < 100; n++)
    {
         i = i++;
    }
    Console.WriteLine(i);
}
VN:F [1.9.22_1171]
Rating: 5.0/5 (6 votes cast)
C# Increment operator (++) question, 5.0 out of 5 based on 6 ratings
  1. July 6th, 2009 at 16:21 | #1

    Little but Interesting Addition!

    VA:F [1.9.22_1171]
    Rating: 5.0/5 (1 vote cast)
  2. Mausi
    July 12th, 2009 at 15:01 | #2

    i guess output will be 200 because the for loops 100 times and
    “i = i++;” can be translated to
    { i=i; i=i+1; }

    VA:F [1.9.22_1171]
    Rating: 1.0/5 (1 vote cast)
  3. Mausi
    July 12th, 2009 at 15:06 | #3

    ok output is 100. i always fail at your tests

    VA:F [1.9.22_1171]
    Rating: 5.0/5 (1 vote cast)
  4. July 12th, 2009 at 15:12 | #4

    @Mausi
    Don’t be sad :)
    I’ve got another one for you : LINQ Teaser

    VN:F [1.9.22_1171]
    Rating: 0.0/5 (0 votes cast)
  5. Krishnakar Thakur
    February 5th, 2011 at 05:48 | #5

    int a=5,p;
    p=a++ + ++a + a++;

    What will be output in C#.
    In c output will be- p=18, a=8 in c++ – p=18, a=8 , in Java p=18, a=8 but in C#- p=19 , a=8 it is wront or right. If right then how can c# work on Increment / Decrement operator

    VA:F [1.9.22_1171]
    Rating: 0.0/5 (0 votes cast)
  6. Vinni
    February 27th, 2011 at 18:59 | #6

    @Xander

    I am new to c#. Can u tell me why increment operator is not working in this case?
    thanks.

    VA:F [1.9.22_1171]
    Rating: 0.0/5 (0 votes cast)
  7. Vinni
    February 27th, 2011 at 19:00 | #7

    @F@!S@L |Qb@L
    I am new to c#. Can u tell me why increment operator is not working in this case?
    thanks.

    VA:F [1.9.22_1171]
    Rating: 0.0/5 (0 votes cast)
  8. Amendra
    March 25th, 2011 at 02:14 | #8

    @Vinni
    The result of the operation w++ is actually the value of the operand (w) itself just be for the increment (++). Since w was 100 before the operation and was assigned back to w, it will always stay 100.

    This is also know as a post fix incremenet.

    If you want it to be incremented and assigned use ++w;

    VA:F [1.9.22_1171]
    Rating: 0.0/5 (0 votes cast)
  9. March 23rd, 2012 at 16:01 | #9

    You should try that

    int a = 2;
    bool c = (a = a++ + ++a – a– + –a * ++a + ++a -2) == 8;

    VA:F [1.9.22_1171]
    Rating: 5.0/5 (1 vote cast)
  10. March 23rd, 2012 at 16:05 | #10

    @Yvan Dubois
    i made a mistake, it should be:

    bool c = ((a = a++ + ++a – a– + –a * ++a + ++a – 2) == 8);

    VA:F [1.9.22_1171]
    Rating: 5.0/5 (4 votes cast)
  1. No trackbacks yet.

Subscribe without commenting

SEO Powered by Platinum SEO from Techblissonline
watch free movies