+ 1
How is this operator += works in c++??
while (num <= 5) { cin >> number; total += number; num++; }
5 odpowiedzi
+ 8
total += number
is
total = total + number
+ 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).
+ 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.
0
Total= total+number