Valerie Meunier

Valerie Meunier

  • NA
  • 693
  • 82.8k

different results with x++ and x+=1

Oct 12 2021 2:00 PM

Hi

Using x++ or x+=1 (both increment with 1) gives different results. How come? 

Thanks

this code gives: 6 7 8 9 10

 int x = 5;
        int i = 0;
        while (i < 5)
        {
            Console.WriteLine(x +=1);
            i++;
        }

But this code gives 5 6 7 8 9

 int x = 5;
        int i = 0;
        while (i < 5)
        {
            Console.WriteLine(x++);
            i++;
        }


Answers (2)