Please,say me,what is the difference between ++a and a++. | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

Please,say me,what is the difference between ++a and a++.

28th Oct 2016, 12:44 PM
David
David - avatar
3 Answers
+ 17
Theres no difference between these lines of code: a++; and ++a; The difference will be important if you assign the value of 'a' to another variable. For example: var a = 5 var b = a++ In this case, 'b' will be 5 because the value of 'a' is being incremented AFTER assigning 'a''s value to 'b'. var a = 5 var b = ++a In this case, 'b' will be 6 because the value of 'a' is being incremented BEFORE assigning its value to 'b'. In both cases, 'a' will be 6 at after assigning its value to 'b'.
28th Oct 2016, 1:39 PM
Dominik Braun
Dominik Braun - avatar
+ 9
++a is generally known as pre increment and a++ as post increment. in pre increment value of 'a' is increase first then it will assign to 'a' back but in post increment value of 'a' is increment at last of the current statement. let me explain in C++ int a=1; cout<<++a; output will be 2 but if i write cout<<a++; output will be 1 and after these statement value of 'a' will be 2
28th Oct 2016, 1:41 PM
AKSHAY MALVIYA
AKSHAY MALVIYA - avatar
0
++a is pre-increment and a++ is post-increment
7th Nov 2016, 2:21 AM
vivek kumar
vivek kumar - avatar