What is difference between a = b++ and a = ++b | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

What is difference between a = b++ and a = ++b

6th Dec 2016, 2:02 PM
Ravi Kumar
Ravi Kumar - avatar
5 Answers
+ 2
the diferent is that by a=b++ a the value of b become and then inkrement. a=++b means tht before the valeue a get the value b will be inkremt and then assigned to a.
6th Dec 2016, 2:16 PM
THDevelop
THDevelop - avatar
+ 1
thank you
6th Dec 2016, 2:15 PM
Ravi Kumar
Ravi Kumar - avatar
+ 1
For a = b++, a would take the value of b first, then b += 1. And a = ++b means that b += 1 first, then a take the value of b. For example let b = 1, after a = b++, a = 1 and b = 2; but after a = ++b, a = 2 and b = 2.
6th Dec 2016, 2:20 PM
Yong Xuan Wang
Yong Xuan Wang - avatar
+ 1
One is post increment (i++) the other is pre increment (++i) lets say we have: i=0; //then de wish to do this addition i=(i++) + (++i); /* what happen... 1. i++ stays 0 as it is post increment. 2. i value becomes 1 3. ++i becomes 2 as it is pre increment. 4. the machine executes it as 0 + 2 5. finally the machine returns answer as 2*/
7th Dec 2016, 6:05 PM
crosiv
crosiv - avatar
0
when b is 2 a=b++ // a is 2, b is 3 a=++b // a is equal to b and b is 3
6th Dec 2016, 2:12 PM
Andreas K
Andreas K - avatar