+ 4

About ++x

Where to use ++x

21st Jun 2020, 6:36 AM
Kartik Mishra
Kartik Mishra - avatar
2 Answers
+ 7
It's pre increment: It will increment the x first. Suppose x=0 If you print (++x + x--): Left to right. It will first increment the x(i.e. ++x part), now x is 1. Then from x-- part it will first assign x(i.e. currently 1) and then decrement(note that after decrement it will become 0). From ++x part it's "1" and from x-- part it's also "1", so it will print "2". Now if you print(x) you will get 0. I don't know C# yet.. but I hope this helps.
21st Jun 2020, 6:52 AM
Blue!!
Blue!! - avatar
+ 1
++x is like a prefix operator. It increments the entered value of x by 1 each time it is called;either once or n times in a loop.. Suppose x=1; So ++x will be equal to 2. Similarly; --x is also a prefix operator. It decrements the entered value of x by 1 each time it is called;either once or n times in a loop.. Suppose x=1; So --x will be equal to 0. x++ and x-- are known as postfix operators and do not affect the entered or submitted value of x. If x=1; x++ will also be equal to 1 and x-- will also be equal to 1. So you use it where you have to shorten your code; like if you have to use x=1 at a certain point in your code; So wherever you need to use the value as 2;you can simply put ++x there instead of having to initializing a new Variable and assign it t be 2.Hence shortening the code and restricting the use of variables.. NOTE : If you use the ++ operator as postfix like: x++. The original value of x is returned first then, x is incremented by 1. Hope this helps!!!
21st Jun 2020, 2:15 PM
Samagra Jain
Samagra Jain - avatar