0

Assignment operators.

#include <iostream> using namespace std; int main() { int num = 1; int number; int total = 0; while (num <= 5) {// Will repeat 5 times. cin >> number;//User will enter a number 5 times. See above. total +=number;/*So 0+the previous number the user has entered every time. = to include the previous number as well. Or we will get 0+number over and over again*/ num++; } cout << total << endl; return 0; } So this one here calculates the sum of the numbers the user has entered 5 times. The only thing I can't seem to get my head round is the += part between the two variables ? Can someone take up the arduous task of explaining what this part is doing ? Many thanks guys.

6th Sep 2018, 12:40 PM
harry
3 Answers
+ 2
It's a shorthand for math operations. For example a = a + b is the same like a +=b, other are a = a - b => a -= b, a = a * b => a *= b, a = a / b => a /=b, a = a % b => a %= b etc.
6th Sep 2018, 1:02 PM
TheWh¡teCat 🇧🇬
TheWh¡teCat 🇧🇬 - avatar
0
It is an short hand operator. int a = a + 1; // can be also written as int a += 1; Same rule applies with - , * , / , % . Hope this helpsâ˜șâ˜ș.
6th Sep 2018, 1:03 PM
Meet Mehta
Meet Mehta - avatar
0
Ah I see. Thank you see much ! Just got my results from Maths exams back and it's only a 4/C lol
6th Sep 2018, 1:08 PM
harry