-4

Please explain ++x in layman's terms

I tried searching for this here, youtube, Google, Chegg, and a programming community on discord. I'm pretty desperate now because I I still don't understand this. haha I know that it's a prefix which means that it increments the value then does the expression, but I don't exactly understand what that means. I also know what y -= 3 means which is y-3.

int x = 8;
int y = ++x;
 if (x > 5)
 y -= 3;
 else
 y = 9;
Console.WriteLine(y);

I don't know what the answer is.

Aurora A
  • 101
  • 1

1 Answers1

-2
int x = 10;
int y = ++x;

now y == 11 and x == 11

int x = 10;
int y = x++;

now y==10 and x == 11

In both cases x is increased by 1. The difference is that when you use ++x it returns the value after increment (increment first and assign second) while x++ returns the value before increment (assign first and increment second).

jjj
  • 575
  • 1
  • 3
  • 16