-7

how does the optput come as 3 3 4 I don't understand how 'i' become 4 for last

code:

int i = 2;
            Console.WriteLine(++i); 
            Console.WriteLine(i++);
            Console.WriteLine(i);
  • 6
    *use the debugger, Luke* – Ňɏssa Pøngjǣrdenlarp Jun 17 '21 at 04:49
  • 3
    [What is a debugger and how can it help me diagnose problems?](https://stackoverflow.com/q/25385173/995714), [How to debug small programs](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/) – phuclv Jun 17 '21 at 05:10
  • 3
    Does this answer your question? [What is the difference between i++ and ++i?](https://stackoverflow.com/questions/3346450/what-is-the-difference-between-i-and-i) – Franz Gleichmann Jun 17 '21 at 05:15
  • 1
    `i++` is a "shorter" form for `i = i + 1` : [Increment operator ++](https://learn.microsoft.com/dotnet/csharp/language-reference/operators/arithmetic-operators#increment-operator-) | [Increment/Decrement Operators](https://www.c-sharpcorner.com/blogs/increment-and-decrement-operators-using-c-sharp-code2) –  Jun 17 '21 at 06:05

3 Answers3

6

Because you first used the prefix increment operator and then used postfix.

int i = 2;
Console.WriteLine(++i); // i became 3 *before it was used*
Console.WriteLine(i++); // i first used, still 3, and then incremented becoming 4
Console.WriteLine(i); // i is still 4
Andrew Falanga
  • 2,274
  • 4
  • 26
  • 51
3

In c# assigning a value to a variable also returns a value. Usually it is the value that is assigned

Here is an assignment, the current time is assigned to the now variable:

DateTime now = DateTime.Now;

This assignment also results in a returned value of the current time. It means you can legally write this:

DateTime now; 
Console.WriteLine(now = DateTime.Now);

Not only will your now variable end up set to the current time, but the value of what was assigned is printed out, so the current time is printed


This is also an assignment:

int i = 2;

And so is this:

++i;

It is short for this:

i = i + 1;

Now because we know assignments return values we know it's legal to print out assignments:

Console.WriteLine(i = i + 1);

If i was 2, the sum on the right would be evaluated to give 3, then 3 would be assigned into i, and then also 3 is returned so 3 prints out in the console


i++ is more like a special case, where the value that i was BEFORE the assignment is made, is printed out. You can think of i++ as working like:

int i_before = i; //remember it
i = i + 1;
return i_before; //return the old value

So this:

Console.WriteLine(i++);

If i is 3 already, it will be incremented to 4, but 3 is returned and printed

The easy way to remember the difference between i++ and ++i is:

"in i++ the i is before the ++ so the value you get returned is what i was before it was incremented"


If you just use the increment as a statement on its own, it makes no difference which you use:

int i = 2;
i++;
++i;
Console.WriteLine(i); //4

It only matters which you use if you are relying on the "assignment returns a value" behavior


By the time the last line of your code comes around, i is 4 because it has been incremented twice, from 2

Caius Jard
  • 72,509
  • 5
  • 49
  • 80
0

i starts out as 2.

On the first line, you (pre) increment it to 3, and then print it out.
On the second line, you print it out and then increment it (post increment), so it's now 4.
On the last line, you just print out the current value, which is 4, as noted above.

Mureinik
  • 297,002
  • 52
  • 306
  • 350