Introduction
You have already written count = count + 1 more than once. Adding to a variable and putting the answer back where it came from is the commonest single line in programming.
Compound operators are a shorthand for exactly that:
count = count + 1; // longhand
count += 1; // compound
count++; // increment — for 1 only
All three do the same thing. The shorthand is not there to save typing so much as to make the intent obvious: += says add to this, in one glance, without you having to check that the variable on the left is the same one on the right.
That check matters more than it sounds. total = subtotal + 1 looks almost identical to total = total + 1, and the difference is a whole bug.
The one to treat carefully is ++. There are two versions, x++ and ++x, and they differ only when you use the value in the same line — which is a good reason not to.
What you will be able to do
By the end of this lesson you can:
- Rewrite an assignment using
+=,-=,*=,/=and%=. - Use
++and--. - Say what
x++gives that++xdoes not. - Recognise why
x = x++is a mistake. - Read compound operators without translating them back first.
What you need
| Part | Type | Qty |
|---|---|---|
| Arduino UNO R3 | Microcontroller | 1 |
| USB A to B cable | Cable | 1 |
Basic
No wiring. Read everything in the Serial Monitor at 9600 baud.
The shorthand
| Compound | Means | With x = 10 |
|---|---|---|
x += 3 | x = x + 3 | 13 |
x -= 3 | x = x - 3 | 7 |
x *= 3 | x = x * 3 | 30 |
x /= 3 | x = x / 3 | 3 |
x %= 3 | x = x % 3 | 1 |
x++ | x = x + 1 | 11 |
x-- | x = x - 1 | 9 |
int score = 0;
score += 10; // 10
score += 5; // 15
score -= 3; // 12
score *= 2; // 24
Serial.println(score);
+= works on the right-hand side as a whole:
x *= 2 + 3; // x = x * (2 + 3) — NOT x = x * 2 + 3
The whole expression on the right is worked out first. Brackets, again, when in doubt.
++ and --
For adding or subtracting exactly one:
count++; // count = count + 1
count--; // count = count - 1
You will see these everywhere, especially in for loops later on.
Challenges
Challenge 1
One variable, five operations.
Start with int x = 100; and print x after each step:
x += 20x -= 5x *= 2x /= 3x %= 7
Predict all five answers before running it. Step 4 is the one to think about.
Log in to ask for the answer.
Challenge 2
Post and pre.
Print each of these and note what appears:
int a = 5;
Serial.println(a++);
Serial.println(a);
int b = 5;
Serial.println(++b);
Serial.println(b);
Both a and b end at 6. Explain in one sentence why the first printed line differs.
Then write the two versions on their own lines, print the result, and confirm they are now identical.
Log in to ask for the answer.
Challenge 3
A score that goes up.
Make a counter that starts at 0 and, once a second:
- goes up by 1
- every 5th count, goes up by an extra 10 as a bonus
- prints the score
Use ++ for the ordinary step, += for the bonus, and % to spot every fifth.
After 10 seconds the score should be 30. Work that out on paper first, then check.
Log in to ask for the answer.
Extra challenge
A rolling average, without arrays.
Keep two variables: a running total and a count.
Every second, generate a random number from 1 to 100, add it to the total with +=, increment the count with ++, and print the number, the total and the average so far.
value 42 total 42 average 42
value 17 total 59 average 29
value 88 total 147 average 49
The average is total / count — and it will be wrong in a specific way. Work out why, then fix it so the average shows one decimal place.
Think about it: this keeps a running average of a thousand readings using two variables. Storing all thousand would take 2000 bytes, which an UNO does not have. What can this version not tell you that the full list could?
Log in to ask for the answer.