IF/ELSE STATEMENTS

This page is part of my Model Remodel series of articles and a subset of my Arduino section.

DISCLAIMER: If you choose to attempt any of these modifications, you assume all risks thereof. I just wanted to share my experiences here. Neither Eaglemoss, nor myself, are responsible for any damages that may occur.


if/else Statements

An if/else statement is a way we can tell our Arduino to evaluate a ‘condition’ to see if it is true or false. If the condition is true, we can execute certain commands. If the condition is false, we can execute different commands. The syntax of a basic if/else statement looks like this:

if (condition) {
 // execute these commands if true
} else {
 // execute these commands if false
}

As an example, we could use an if/else statement to verify the value of our LEDPin variable is 3 before turning the LED on. If you only need to check for a true condition, the else section of the statement is optional.

NOTE: I will be using an ellipsis (…) in these code snippets to indicate where I have hidden part of the sketch so we can focus on one section. These ellipsis are not part of the actual sketch and should not be included in your programming.

...
void loop() {
  // Check to see if the LEDPin variable is equals to 3
  if (LEDPin == 3) {
    // Turn Pin 3 on
    digitalWrite(LEDPin,HIGH);
  }
}

Comparison Operators

You can see that I used two equals signs (==) in my if/else statement condition above. This is called a Comparison Operator. It tells the Arduino to compare the two values (operands) on either side of the Comparison Operator and evaluate if they are exactly equal. If they are, the condition evaluates to true and the commands inside the if statement will be executed.

There are many Comparison Operators available to us:

Operator nameOperator simpleDescriptionExample
equal to==Checks if the value of two operands is equal or not, if yes then condition becomes true.(A == B) is not true
not equal to!=Checks if the value of two operands is equal or not, if values are not equal then condition becomes true.(A != B) is true
less than<Checks if the value of left operand is less than the value of right operand, if yes then condition becomes true.(A < B) is true
greater than>Checks if the value of left operand is greater than the value of right operand, if yes then condition becomes true.(A > B) is not true
less than or equal to<=Checks if the value of left operand is less than or equal to the value of right operand, if yes then condition becomes true.(A <= B) is true
greater than or equal to>=Checks if the value of left operand is greater than or equal to the value of right operand, if yes then condition becomes true.(A >= B) is not true

Download my Sketch

To help others, I have shared this updated sketch via the Arduino Editor. Simply click the Open Code or Download buttons!

Using if/else statements is a fundamental part of any programming – my final sketch uses them all over the place. Becoming very familiar with them will benefit your learning, so feel free to try various ways of implementing them to do what you need. Next, we will use everything we have learned so far to go a step further and make our LED flash on and off.

Next Section


TIMING – Using the Arduino to time events

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.