Arduino if Statement

Ammar Ali Oct 12, 2023
  1. Using the if Statement With Comparison Operators in Arduino
  2. Using if Statement With boolean Operators in Arduino
Arduino if Statement

In this tutorial, we will discuss the use of the if statement to check for different conditions in Arduino.

Using the if Statement With Comparison Operators in Arduino

The if statement is used to check different conditions, if the condition is true, the code inside the if statement parenthesis will be executed; otherwise, not. The input parameter of an if statement is a boolean which can eighter be true or false. The basic syntax of the if statement is given below.

void loop() {
  if (condition) {
    your code;
  }
}

In the above code, the condition is a boolean. If the condition is true, the code inside the if statement will be executed; otherwise, not. Now how can we pass conditions in the if statement using the comparison operators? The comparison operators include six operators that are equal to, not equal to, less than, less than or equal to, greater than, greater than, or equal to. We can compare two or more variables inside the if statement using the comparison operators. Consider we want to compare two variables, and if the two variables are equal, we will execute the code; otherwise, not.

int a = 10;
int b = 10;

void setup() { Serial.begin(9600); }
void loop() {
  if (a == b) {
    Serial.print("a is equal to b");
  }
  if (a < b) {
    Serial.print("a is less than b");
  }
}

In the above code, we compare two variables, a and b. You can see that the value of the two variables is equal, so only the first if statement will be executed because the condition is true. The second statement will not be executed because the condition is false. You will only see the first statement printed on the serial monitor, which is a is equal to b. Now, if we want to compare more than two variables using the if statement, then we have to use the boolean operators.

Using if Statement With boolean Operators in Arduino

The boolean operators are logical AND, logical NOT, and logical OR. We can use these operators to put more than one condition in the if statement. If we want the statement to be executed only if all the conditions are true, then we have to use the logical and operator. If we want the statement to be executed even if one of the conditions is true, then we have to use the logical or operator. Now consider an example where we want to put multiple conditions inside the if statement.

int a = 10;
int b = 10;
int c = 20;

void setup() { Serial.begin(9600); }
void loop() {
  if (a == b && a < c) {
    Serial.print("Something");
  }
  if (a < b || a < c) {
    Serial.print("SomethingElse");
  }
}

In the above code, the first if statement will be executed because both of the conditions are true, and you will see Something printed on the serial monitor. In the second if statement, one condition is true and one is false, but it will also be executed because we have to use the logical or operator that means even if one of the two conditions is true, the statement will be executed, and you will see SomethingElse printed on the serial monitor.

Author: Ammar Ali
Ammar Ali avatar Ammar Ali avatar

Hello! I am Ammar Ali, a programmer here to learn from experience, people, and docs, and create interesting and useful programming content. I mostly create content about Python, Matlab, and Microcontrollers like Arduino and PIC.

LinkedIn Facebook