If Statements
Within our code, we want to be able to make decisions about how to respond to readings from a sensor or when a user has pushed a button for example. We can detect a change by using digitalRead
or analogRead
and this will give us a value in our code that we can examine. To make a decision on what to do we can use something called if
statements. These are moment in our code where we inspect the contents of a variable using logical comparisons and make decisions about what code to execute.
It looks a little bit like this:
// read in a value from the button pin
int buttonReading = digitalRead( buttonPin);
// if the button is high
if( buttonReading == HIGH ){
// execute the code in between the { and }
}
An if statement will begin with the word if
in our code. The next thing it needs is the comparison to make. In this case, you’ll see a double equals (==
). This is a test for equality or in other words that the value on the left hand side is an exact match for the value on the right hand side.
But there are a range of comparisons that you can make. They include:
==
- is equal to!=
- is not equal to<
- is less than>
- is greater than<=
- is less than or equal to>=
- is greater than or equal to
or else
If we have an if
statement on its own, we must test for a condition, and if that condition is met the code wrapped by the curly brackets will be executed.
But we can also create a condition that is executed by default if the condition is not met. This allows us to create branches in our code. We can do this by adding an else
statement to our original if
For example, if we wanted to read from our button pin and either turn a LED on or off in response to the button, we could write something like this:
// read in a value from the button pin
int buttonReading = digitalRead( buttonPin);
// if the button is high
if( buttonReading == HIGH ){
digitalWrite( ledPin, HIGH );
}else{
digitalWrite( ledPin, LOW );
}
Many conditions
You can add lots of conditions to your if statement. If you want to test multiple conditions, you can use an else if{}
statement to chain these in.
For example
int value = 32;
if( value < 15 ){
// do some stuff here
}else if( value >= 15 ){
// do some stuff here
}else{
// do some other stuff
}
An else if
must be preceded by an if
statement. Equally an else
statement must also be preceded by an if
. An else if
block doesn’t have to be followed by an else
.
You can use as many else if
statements as you would like. But it’s important to know that the else if
s will be checked in the order they’re written, that means the first sequential condition that is met will be the block of code executed. It won’t continue to evaluate any more conditions after one is met. Only if no conditions are met, will an else
block be performed.
Complex conditions
When writing if
statements and conditional logic, you can evaluate more complex conditions using or
and and
- If you use
and
, it will allow you to chain together multiple conditions and all of those conditions must be met in order for the code to exeucte - If you use
or
, it will let you chain together conditions and will run the code block if any of the conditions match.
For example
if( buttonReading == HIGH and counter < 10 ){
// only happens if the button is HIGH
// and the counter is less than 10
counter++; // add one to counter
}
// or to use an or
if( buttonReading == HIGH or counter < 10 ){
// happens if either the buttonReading is HIGH
// or the counter is less than 10
counter++; // add one to counter
}
Learn more
- Conditions - Arduino Language Reference
- If - Arduino Language