How is this operator += works in c++?? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

How is this operator += works in c++??

while (num <= 5) { cin >> number; total += number; num++; }

22nd Sep 2017, 1:00 PM
Md. Amin Kaiser
Md. Amin Kaiser - avatar
4 Answers
+ 8
total += number is total = total + number
22nd Sep 2017, 1:06 PM
Kartikey Sahu
Kartikey Sahu - avatar
+ 4
the operator += in this example is the same as writing total = total + number; it's just a shortcut, it makes the code look better and respects one of the most fundamental rules when programming. the DRY rule (aka Don't Repeat Yourself).
22nd Sep 2017, 1:09 PM
OUMAS Abdellah
OUMAS Abdellah - avatar
+ 2
Here, total+=number : it is, total = total + number. it is a shorter way to express it. and number ++: this add the number by one and again enter the loop and this will carry on until the given statement in the while statement becomes incorrect.
22nd Sep 2017, 1:08 PM
Suriya
Suriya - avatar
0
Total= total+number
23rd Sep 2017, 6:59 AM
Shivneep Brar
Shivneep Brar - avatar