New course! Every coder should learn Generative AI!
Try a free lesson+ 2
Please,say me,what is the difference between ++a and a++.
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'.
+ 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
0
++a is pre-increment and a++ is post-increment