In a loop in C++, I usually encounter situations to use ++ or +=1, but I can’t tell their difference. For instance, if I have an integer
int num = 0;
and then in a loop I do:
num ++;
or
num += 1;
they both increase the value of num, but what is their difference? I doubt num++ could work faster than num+=1, but how? Is this difference subtle enough to be ignored?
num += 1is rather equivalent to++num.All those expressions (
num += 1,num++and++num) increment the value ofnumby one, but the value ofnum++is the valuenumhad before it got incremented.Illustration:
Use whatever pleases you. I prefer
++numtonum += 1because it is shorter.