I confuse about ++a and a++? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

I confuse about ++a and a++?

Help me please

14th Oct 2016, 3:37 PM
Can
Can - avatar
3 Answers
+ 2
Both commands do the same, add 1 into variable a like if you write a=a+1; ++a and a++ are the same when it is a standalone command. But there is a big difference, when you combine it with another command. For example : b=a++; or b=++a; or in conditions if, or while, etc.. a++ add 1 after exetution this command, --a before it, So results are different. Try it
14th Oct 2016, 4:14 PM
Petr Hatina
Petr Hatina - avatar
+ 2
when you use pre increment (++a) it first add 1 to a and then use the new value of a for the operations, in other hand on post increment (a++), it first uses the current value of a for the operations and then add 1 to the value of a.
14th Oct 2016, 8:07 PM
Badarinarayana Acharya N S
Badarinarayana Acharya N S - avatar
+ 1
literally x =1; ++x will make x equals to 2 and x++ will makes x=1 and next time x=2
14th Oct 2016, 8:50 PM
waleed bin khalid
waleed bin khalid - avatar