Arduino if Statement

  1. Using the if Statement With Comparison Operators in Arduino
  2. Using if Statement With boolean Operators in Arduino

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 who 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.

void setup(){
    int a = 10;
    int b = 10;
    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.

void setup(){
    int a = 10;
    int b = 10;
    int c = 20;
    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.

Contribute
DelftStack is a collective effort contributed by software geeks like you. If you like the article and would like to contribute to DelftStack by writing paid articles, you can check the write for us page.