Difference between ++a and a++ | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 3

Difference between ++a and a++

19th Jan 2017, 6:57 AM
Pragati Porwal
Pragati Porwal - avatar
5 Answers
+ 3
This is what called Operator precedence. Operator precedence determines the order in which operators are evaluated. Operators with higher precedence are evaluated first. A common example: 3 + 4 * 5 // returns 23 The multiplication operator ("*") has higher precedence than the addition operator ("+") and thus will be evaluated first. Complete description of operator precedence in Javascript is here: https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Operators/Operator_Precedence
19th Jan 2017, 8:05 AM
Ahmad
Ahmad - avatar
+ 2
int a = 2 int b = ++a -> b = 3 and a = 3 int b = a++ -> b = 2 and a = 3
19th Jan 2017, 7:01 AM
Kawaii
Kawaii - avatar
+ 1
I'm assuming C# took this from C, so here goes: ++a is pre-increment, and a++ is post-increment. The difference lies in which value is used in the rest of the expression containing it. With ++a, a is incremented first, and that is the value you get; with a++ you get the value of a, and a is incremented afterwards. N3tW0rK's answer illustrates it perfectly. If, on the other hand, your only aim is incrementing a and the value isn't being used, there is effectively no difference between them, and which one you use is largely a matter of personal taste.
19th Jan 2017, 8:52 PM
Tim Spalding
Tim Spalding - avatar
+ 1
what the difference between a++ and ++a ++a : it means increments then asign the value ex: int a=3; int y=++a; now y = 4 and also a=4 a++ : it means asign then increment the value ex: int a=3; int y=a++; now y=3 and a=4
20th Jan 2017, 3:47 PM
Ahmad Hamdan
Ahmad Hamdan - avatar
0
GGG
30th Oct 2023, 4:14 PM
Thawab Mu
Thawab Mu - avatar