What are operators?
Operators are special characters, or sets of charactors, that perform a specific action to elements of your program such as variables. You’ll already be familiar with common operators like + and – used for addition and subtraction and we’ll explore everything else below.
Why do we need operators?
Without operators, you can’t assign values to variables and, even if you could, you wouldn’t be able to to any calculations with them. We need operators to make calculations, assign values to variables and to compare values.
The different types
There are several types of operators. This series of posts is aimed at helping people to get started, so I’m going to cover the most useful of them. I’ll cover the following types: arithmetic, assignment, increment / decrement, and relational.
Basic arithmetic operators
As mentioned above, you’ll be familiar with + and -, but I’ll show you how to use those with your variables. Of course, we can multiply and divide too, but those operators have slightly different characters to what you might be familiar with; * and / respectively. There’s also the modulus operator which is pretty handy to know when you’re programming.
Let’s demonstrate these operators in use with some code:
// We'll declare two variables outside of the loop() function because we don't want them to keep reseting as the loop runs.
// loopCycles will be used to count how many times the loop() function has run.
int loopCycles = 1;
// waitTime will be used to set the delay() between messages. Remember, the delay() function expects a value in milliseconds.
int waitTime = 5000;
// The setup() function runs only once after power on / reset
void setup() {
// Initialise serial communication at 9600 bits per second
Serial.begin(9600);
}
// The loop() function runs continuously after the setup() function has completed.
void loop() {
// We'll declare our other variables inside the loop() function because we don't mind them reseting on each run.
int firstNumber = 5;
int secondNumber = 10;
int thirdNumber = 23;
// We'll use Serial.print and to display variable values in the Serial Monitor.
// Note that \n ,despite being two characters, is recognised as one character and it represents a new line. When we're using Serial.print, it doesn't add a new line automatically, so we have to do it ourselves.
// Anything in quotes is seen as a string of characters and will be printed as typed.
Serial.print("The current loop cycle count is ");
// When you try to print a variable name without using quotes, the contents of the variable will be displayed.
Serial.print(loopCycles);
// We're using quotes again here, so we're printing what we've typed. Remember that special character I mentioned earlier?
Serial.print(".\n");
// Let's slow things down with a delay. Remember the waitTime variable we set at the beginning of our code?
delay(waitTime);
// Let's display our other variables:
Serial.print("firstNumber is ");
Serial.print(firstNumber);
Serial.print(".\n");
delay(waitTime);
Serial.print("secondNumber is ");
Serial.print(secondNumber);
Serial.print(".\n");
delay(waitTime);
Serial.print("thirdNumber is ");
Serial.print(thirdNumber);
Serial.print(".\n");
delay(waitTime);
// Now, let's use some arithmetic operators:
Serial.print("firstNumber + secondNumber is: ");
Serial.print(firstNumber + secondNumber);
Serial.print("\n");
delay(waitTime);
// Easy, right? Let's continue:
Serial.print("secondNumber - firstNumber is: ");
Serial.print(secondNumber - firstNumber);
Serial.print("\n");
delay(waitTime);
// Now for some multiplication and division:
Serial.print("secondNumber x thirdNumber is: ");
Serial.print(secondNumber * thirdNumber);
Serial.print("\n");
delay(waitTime);
Serial.print("secondNumber ÷ firstNumber is: ");
Serial.print(secondNumber / firstNumber);
Serial.print("\n");
delay(waitTime);
// Finally, let's check out the modulus operator.
// Modulus returns the remainder of a division. For example, 5 / 2 is 2 remainder 1 (there are two 2s in 5 and 1 is left over). 5 modulus 2 is 1. The remainder. The symbol for modulus is %.
Serial.print("thirdNumber % secondNumber is: ");
Serial.print(thirdNumber % secondNumber);
Serial.print("\n");
delay(waitTime);
// There's one last thing to do. We've completed our loop, so we need to increment our loopCycles variable by 1. There are a couple of ways to do this so I'll try to make it as easy as possible to see how this works.
loopCycles = loopCycles + 1;
// This line is assigning loopCycles a new value.
// On the left, we have the variable we're assigning a value to.
// On the right, we have the value we're assigning to it.
// At the point where this line is executed for the first time, loopCycles is 1 because it's still in the state we left it when we assigned it.
// So, we're assigning to the variable loopCycles the value that loopCycles currently holds plus 1.
// That'll be 1 + 1 on the first cycle.
// It's the same as typing loopCtcles = 2; only, the method we're using here will continue to increment the number on each cycle.
// When loopCycles is 2, the code will be assigning 2 + 1 to the variable and so on.
}
The above example covers the arithmetical operators and demonstrates some common uses of variables. For example, if you wanted to speed things up, you could assign a smaller number to the waitTime variable.
I’ve also deliberately left initialisation of the waitTime variable outside of the loop() function so that you can add a line of code to speed the program up while it’s running. I’ll leave you to figure that one out.
Assignment operators
We’ve already worked with the mopst used assignment operator, ‘=’.
This assigns the value on the right to the variable on the left:
// Initialise a variable named a and assign it the value 1:
int a = 1;
// Change the value held by variable a by assigning a new value:
a = 2;
It’s also possible to perform an arithmetic action at the same time as assignment. For example, you can increment the value held in avariable by a number by using the += value:
// Increment the value held in a by 2:
a += 2;
// This is the equivelant of: a = a + 2;
// If a was 1 before, it's now 3
This works with the other arithmetic operators too:
a -= 2;
// This is the equivelant of: a = a - 2;
// If a was 1 before, it's now -1
a *= 2;
// This is the equivelant of: a = a * 2;
// If a was 1 before, it's now 2
a /= 2;
// This is the equivelant of: a = a / 2;
// If a was 10 before, it's now 5.
a %= 2;
// This is the equivelant of: a = a % 2;
// If a was 5 before, it's now 1
Increment / decrement operators
Increment operators aren’t technically necessary, but they’re a handy shortcut.
you can either increment a variable or decrement a variable. This can be done before or after using the variable’s value. I know that probably doesn’t make sense, so let’s see some examples. You can run this code as it is in your Arduino and watch the operators in action.
int waitTime = 5000; // Change this to speed things up or slow them down
void setup() {
Serial.begin(9600);
}
void loop() {
int variable = 1;
// Let's print out the variable to the Serial Monitor:
Serial.println("START: variable is currently:");
Serial.println(variable);
// This is 'using' the variable
delay(waitTime);
// We can increment the variable with the increment operator:
variable++;
Serial.println("Incremented variable:");
Serial.println(variable);
delay(waitTime);
// We can decrement the variable with the decrement operator:
variable--;
Serial.println("Decremented variable:");
Serial.println(variable);
delay(waitTime);
// We can 'use' the variable and increment or decrement it at almost the same time:
Serial.println("Increment and use:");
Serial.println(++variable);
// Note how the ++ comes before the variable. This means the variable is incremented and the the value is passed to Serial.println() to be printed.
delay(waitTime);
// Next, we'll use the variable first and then increment:
Serial.println("Use and increment:");
Serial.println(variable++);
delay(waitTime);
// Let's check what the variable is holding:
Serial.println("variable is currently:");
Serial.println(variable);
delay(waitTime);
// You can do the same with -- too ( by using --variable and variable-- instead of ++variable and variable++ )
Serial.println("End of loop, restarting in 10 seconds\n");
delay(10000);
}
Relational / conditional operators
Relational operators, also known as conditional operators, are used when we’re comparing two values. For example, we might want to check if the level of light in a room is low enough to turn on a lamp, or, if the tempearature is warm enough to turn on a fan.
We’ll look at how we make use of these operators in the next post about conditional statements.
The key learning here is that, just like arithmetic operators return a value (e.g. 1 + 1 returns 2), relational operators do too. They return either a 1 or a 0, representing true or false. This is important as it allows for your program to make decisions based on the values in variables.
The relational operators are:
Operator | Description | Example |
Equal to == | Checks if the two values are equal. | 10 == 10; returns 1 5 == 10; returns 0 |
Not equal to != | Checks if the two values are not equal | 10 != 10; returns 0 5 != 10; returns 1 |
Greater than > | Checks if the value on the left of the operator is greater than the value on the right | 5 > 10; returns 0 10 > 10; returns 0 15 > 10; returns 1 |
Greater than or equal >= | Checks if the value on the left of the operator is greater than or equal to the value on the right | 5 >= 10; returns 0 10 >= 10; returns 1 15 >= 10; returns 1 |
Less than < | Checks if the value on the left of the operator is less than the value on the right | 5 < 10; returns 1 10 < 10; returns 0 15 < 10; returns 0 |
Less than or equal <= | Checks if the value on the left of the operator is less than or equal to the value on the right | 5 <= 10; returns 1 10 <= 10; returns 1 15 <= 10; returns 0 |
Let’s see these operators in action. You can paste this code into the Arduino IDE and upload it. Remember to open the Serial Monitor tool to see the outputs. I encourage you to play with the code to test your understanding too
int waitTime = 20000;
void setup() {
Serial.begin(9600);
}
void loop() {
// Let's create a couple of variables
int variable1 = 10;
int variable2 = 20;
// Let's print out the variables to the Serial Monitor:
Serial.println("START: variable1 is currently:");
Serial.println(variable1);
Serial.println("START: variable2 is currently:");
Serial.println(variable2);
delay(waitTime);
// Now we'll go through some examples for each relational variable:
// == (is equal)
// This operator is used to check if two values are equal:
Serial.println("5 is equal to 5 (5 == 5), 1 is 'true' 0 is 'false'");
Serial.println(5 == 5);
Serial.println();
Serial.println("10 is equal to 5 (10 == 5)");
Serial.println(10 == 5);
Serial.println();
Serial.println("Is variable1 20?");
Serial.println(variable1 == 20);
Serial.println();
Serial.println("Is variable2 20?");
Serial.println(variable2 == 20);
delay(waitTime);
// != (is not equal)
// This operator is used to check if two values are equal:
Serial.println("5 is 'not equal' to 5 (5 != 5), 1 is 'true' 0 is 'false'");
Serial.println(5 != 5);
Serial.println();
Serial.println("10 is 'not equal' to 5 (10 != 5)");
Serial.println(10 != 5);
Serial.println();
Serial.println("Is variable1 'not equal' to 20?");
Serial.println(variable1 != 20);
Serial.println();
Serial.println("Is variable2 'not equal' to 20?");
Serial.println(variable2 != 20);
delay(waitTime);
// > (is greater than)
// This operator is used to check if the value to the left of the operator is greater than the value to the right:
Serial.println("5 is greater than 5 (5 > 5), 1 is 'true' 0 is 'false'");
Serial.println(5 > 5);
Serial.println();
Serial.println("10 is greater than to 5 (10 > 5)");
Serial.println(10 > 5);
Serial.println();
Serial.println("Is variable1 greater than 20?");
Serial.println(variable1 > 20);
Serial.println();
Serial.println("Is variable2 greater than 20?");
Serial.println(variable2 > 20);
delay(waitTime);
// >= (is greater than or equal to)
// This operator is used to check if the value on the left of the operator is greater than or equal the value on the right:
Serial.println("5 is greater than or equal to 5 (5 >= 5), 1 is 'true' 0 is 'false'");
Serial.println(5 >= 5);
Serial.println();
Serial.println("10 is greater than or equal to 5 (10 >= 5)");
Serial.println(10 >= 5);
Serial.println();
Serial.println("Is variable1 greater than or equal to 20?");
Serial.println(variable1 >= 20);
Serial.println();
Serial.println("Is variable2 greater than or equal to 20?");
Serial.println(variable2 >= 20);
delay(waitTime);
// < (is less than)
// This operator is used to check if the value to the left of the operator is less than the value to the right:
Serial.println("5 is greater less 5 (5 < 5), 1 is 'true' 0 is 'false'");
Serial.println(5 < 5);
Serial.println();
Serial.println("10 is less than to 5 (10 < 5)");
Serial.println(10 < 5);
Serial.println();
Serial.println("Is variable1 less than 20?");
Serial.println(variable1 < 20);
Serial.println();
Serial.println("Is variable2 less than 20?");
Serial.println(variable2 < 20);
delay(waitTime);
// <= (is less than or equal to)
// This operator is used to check if the value on the left of the operator is less than or equal to the value on the right:
Serial.println("5 is less than or equal to 5 (5 <= 5), 1 is 'true' 0 is 'false'");
Serial.println(5 <= 5);
Serial.println();
Serial.println("10 is less than or equal to 5 (10 <= 5)");
Serial.println(10 <= 5);
Serial.println();
Serial.println("Is variable1 less than or equal to 20?");
Serial.println(variable1 <= 20);
Serial.println();
Serial.println("Is variable2 less than or equal to 20?");
Serial.println(variable2 <= 20);
delay(60000);
}
Further reading
This is one post of a series of introductory posts to Arduino. If you haven’t already checked out the other posts, see: