In this article, we’re going to learn about compound operators and how to use them in Arduino programming. Compound operators are a shorthand way to do simple math with variables. They’re good to know because they’ll make your code more efficient, and you’re sure to see them in other sketches.

Watch the video for this tutorial here:

The 3-in-1 Smart Car and IOT Learning Kit from SunFounder has everything you need to learn how to master the Arduino. It includes all of the parts, wiring diagrams, code, and step-by-step instructions for 58 different robotics and internet of things projects that are super fun to build!

Types of Compound Operators

There are six compound operators we can use with the Arduino:

  • Increment Operator
  • Decrement Operator
  • Compound Addition
  • Compound Subtraction
  • Compound Multiplication
  • Compound Division

In this article, we will go down the list and look at each type of operator and how to use it.

The Increment Operator

Say you have a variable called x, and want to increase its value by one. The long hand way of writing the code is like this:

x = x+1

Instead you can use the increment operator like this:

x++

The ++ is the increment operator. When the increment operator is placed after the variable as above, x is incremented by one, but the original value of x is stored in x.

When the ++ is placed before the variable like this:

++x

x is incremented by one, and the new value of x is stored in x.

To understand this better, take a look at this sketch:

void setup() {
  Serial.begin(9600);

  int x = 0;

  int y = 1 + (x++);

  Serial.print(y);
}

void loop() {
}

We have an int variable called x that is set equal to zero. Then we have another int variable called y that is set equal to 1 + (x++). Then we print the value stored in y to the serial monitor with a Serial.print() function.

If you run this sketch, the number one will be printed to the serial monitor. This is because the math in y = 1 + (x++) is performed before x is incremented. Initially, x is equal to zero, so 1 + 0 = 1.

If you change x++ to ++x, the number two will be printed to the serial monitor. In this case, the math is performed after x is incremented. So now with x equal to one, 1 + 1 = 2.

The Decrement Operator

You can also subtract one from a variable using the decrement operator. The normal code for decrementing a variable is this:

x = x-1;

The decrement operator does the same thing:

x--

Just like the increment operator, when the -- is placed after the variable, math is performed before the variable is decremented. For example, take a look at the same sketch using the decrement operator:

void setup() {
  Serial.begin(9600);

  int x = 0;

  int y = 1 + (x--);

  Serial.print(y);
}

void loop() {
}

Here, x is set equal to zero, so y will equal one since 1 + 0 = 1. The math is performed before the variable is incremented.

In contrast, when the decrement operator is used as --x, the math is performed after the variable is decremented.

Using --x in the sketch above makes y equal to zero. Since x is decremented before the math is performed, the formula becomes y = 1 + (-1), which is zero.

Only int and long data types can be used with the increment and decrement operators. Floats, bytes, and chars won’t work. But for the rest of the compound operators we’re going to talk about you can use any data type.

The Compound Addition Operator

Another really useful compound operator is the compound addition operator. It’s written with a += sign like this:

x += y;

The compound addition operator takes a variable and adds another variable to it, then stores the result in the first variable. For example, writing x += y is the same as writing x = x + y. Take a look at this code for example:

void setup() {
  Serial.begin(9600);

  int x = 2;
  int y = 4;

  x += y;

  Serial.print(x);
}

void loop() {
}

In the code above, x is set equal to two and y is set equal to four. Then we use the compound addition operator x += y to add the y variable to the x variable. The value stored in x is then printed to the serial monitor with a Serial.print() function. If you run this code, the number six should be printed to the serial monitor since 2 + 4 = 6.

The Compound Subtraction Operator

There’s also a compound subtraction operator, which does the same thing as writing this:

x = x - y;

Compound subtraction takes a variable and subtracts another variable from it. The result is stored in the first variable. Compound subtraction is written with a -= like this:

x -= y;

Using x -= y in the sketch above will print a -2 to the serial monitor. Since x equals two and y equals four, 2 – 4 = -2.

The Compound Multiplication Operator

The compound multiplication operator is written with an asterisk and an equals sign:

x *= y;

The compound multiplication operator takes a variable and multiplies it by another variable, then stores the result in the first variable. It’s the same as writing this: 

x = x * y; 

If you use x *= y in the sketch above, the number eight will be printed to the serial monitor since 2 * 4 = 8.

The Compound Division Operator

The compound division operator is written with a forward slash and an equals sign:

x /= y;

It’s equivalent to writing this:

x = x / y; 

The compound division operator takes a variable, divides it by another variable, then stores the result in the first variable.

So if you use x /= y in the sketch above, the result would be 0.5 since 2 / 4 = 0.5. But 0.5 isn’t an integer, it’s a floating point value. So you’ll also need to change the x and y variables from ints to floats for the code to work.

Hope this makes it easier for you to use compound operators when programming Arduino. Feel free to leave any questions in the comment section below.