What is the difference between “++” and “+= 1 ” operators?

num += 1 is rather equivalent to ++num.

All those expressions (num += 1, num++ and ++num) increment the value of num by one, but the value of num++ is the value num had before it got incremented.

Illustration:

int a = 0;
int b = a++; // now b == 0 and a == 1
int c = ++a; // now c == 2 and a == 2
int d = (a += 1); // now d == 3 and a == 3

Use whatever pleases you. I prefer ++num to num += 1 because it is shorter.

Leave a Comment

tech