Increment | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Increment

I don't understand the ++a

26th Dec 2017, 12:06 AM
Victor Jonah
Victor Jonah  - avatar
2 Answers
+ 6
It increments the value of a by 1, instead of writing a = a + 1 or a += 1, likely because it's a common operation. The two forms are ++a and a++ which do slightly different things. The first one increments the value before an operation, the other one after, e.g. int a = 5; int x = a++; (now x is 5 and a is 6) vs int a = 5; int x = ++a (now x and a are 6) because in case 2 the value of a was increased before the assignment (btw I just naturally defaulted to Java syntax but the idea should be clear, just think var instead of int or something)
26th Dec 2017, 12:15 AM
Dan Walker
Dan Walker - avatar
+ 5
increment is done by two ways pre increment and post increment as explained above by @Dan for more just go through this ++x increments the value of x before using it x++ first uses the value of x and then increments it Eg 1: x=1 b=++x <-- increment x before assigning to b print b <--- this will return 2 print x <---this will return 2 Eg 2: x=1 b=x++ <-- first assign x to b then increment x print b <--- this will return 1 print x <---this will return 2 https://www.sololearn.com/Discuss/407846/?ref=app
26th Dec 2017, 1:55 AM
GAWEN STEASY
GAWEN STEASY - avatar