header banner
Default

How does an Arduino if-else statement work?


Running program code based on conditions is a fundamental part of development. It’s also a great way to get into Arduino programming.

Arduino logo

There are many core commands found in modern programming languages. If-else statements are one of the most common you will find, featuring in both simple and complex applications.

But how do you use an if-else statement with an Arduino?

What Programming Languages Do Arduinos Use?

Most Arduino programming involves the Arduino IDE. But what programming language do Arduinos use? The compiler works with a custom version of C++ called the Arduino Programming Language. While it has additional class libraries and basic features, this language uses the same syntax for standard commands like if-else statements.

You can use other compilers to upload scripts in other languages, like Python, to your Arduino. This is the case with many microcontrollers, and there are many microcontroller programming languages available today.

How Does an if-else Statement Work on Arduino?

If statements work by only triggering code when certain conditions are true. You can add an else block to an if statement; it will run if the original condition is not met.

How to Write an if-else Statement on Arduino

Arduino if-else statements use C++ syntax, making them incredibly easy to write. This syntax is similar, if not identical, to that of many other languages.

The following if-else statement tests to see if reality is intact by checking that one equals one.

 void loop() {
   if (1 == 1) {
      Serial.println("Nothing to worry about!");
   } else {
      Serial.println("Uh oh.");
   }
}

As expected, one always equals one, and this means that the if condition is always met, printing a reassuring message. If reality were broken and the maths didn’t add up, though, the else statement would trigger and print a warning.

How to Use Multiple Conditions With Arduino if-else Statements

If-else statements can have multiple conditions to test before they trigger. You can describe the relationship between such conditions using the AND and OR operators. This if-else statement checks the integrity of reality while also checking if a boolean variable is set to true.

 bool Variable = true;

void loop() {
   if (1 == 1 && Variable == true) {
      Serial.println("Nothing to worry about!");
   } else {
      Serial.println("Uh oh.");
   }
}

This example uses the AND (&&) operator which means that the if statement will only trigger if both conditions are true.

 bool Variable = true;

void loop() {
   if (1 == 1 || Variable == true) {
      Serial.println("Nothing to worry about!");
   } else {
      Serial.println("Uh oh.");
   }
}

If you swap this for an OR (||) operator, the if statement will trigger if either or both of the conditions are true.

How to Add Follow-Up Conditions With Arduino else-if Statements

As the final stage in your if-else statement’s journey, it’s time to add some follow-up conditions. You can do this by turning the else statement into an else-if statement:

 bool Variable = true;

void loop() {
   if (1 == 1) {
      Serial.println("Nothing to worry about!");
   } else if (Variable == true) {
      Serial.println("Uh oh.");
   }
}

Much like the previous statements, the main if statement triggers if one equals one. If this isn’t the case, the else statement will only trigger if the bool variable is set to true.

What Can You Use if-else Statements For?

If-else statements are widespread in programming. They offer an easy way to add conditional logic to your Arduino code, making them useful for a huge variety of tasks. This type of command isn’t just found in the Arduino Programming Language, though; almost every modern programming language features if and if-else statements.

Arduino switch…case: An Alternative to if-else Statements

While if-else statements are great, they’re not always the best choice for every operation. If you just need to check the value of a variable and trigger code accordingly, a switch…case statement is a more efficient way to achieve this goal.

 int Variable = 1;

void loop() {
    switch (Variable) {
      case 1:
        Serial.println("It's One!");
        break;

      case 2:
        Serial.println("It's Two!");
        break;

      case 3:
        Serial.println("It's Three!");
        break;

      default:
        Serial.println("It's a number!");
        break;
    }
}

This switch statement checks an integer variable’s value, producing different results if the number is 1, 2, or 3. There is also a default case that will trigger if none of the others trigger, much like the else portion of an if statement.

Learn Arduino Coding Fundamentals

Learning how to use if-else statements in your Arduino code is a big step toward becoming an Arduino master, but there’s much more to learn. While and for loops, logical operators, and a wide range of other fundamentals will help you to push your Arduino code even further.

Sources


Article information

Author: Isaac Smith

Last Updated: 1703683803

Views: 528

Rating: 4.4 / 5 (87 voted)

Reviews: 82% of readers found this page helpful

Author information

Name: Isaac Smith

Birthday: 2009-10-01

Address: 197 Smith Extensions, Jonesbury, WI 40785

Phone: +4271381327654820

Job: Article Writer

Hobby: Drone Flying, Role-Playing Games, Running, Fencing, Card Games, Web Development, Aquarium Keeping

Introduction: My name is Isaac Smith, I am a radiant, bold, expert, lively, Adventurous, multicolored, irreplaceable person who loves writing and wants to share my knowledge and understanding with you.