Learn to code

Arduino Home

Lessons 1
Arduino - Getting Started
Lessons 2
Arduino - Basics
Lessons 3
Arduino - Serial Communication
Lessons 4
Arduino - Digital / Analog
Lessons 5
Arduino - Visual Output
Lessons 6
Arduino - Motor Control
Lessons 7
Arduino -LCD Displays
Lessons 8
Arduino -LCD Displays

Understanding If / Else statement in Arduino

in Arduino Basics

You want to execute a block of code only if a particular condition is true. For example, you may want to light an LED if a switch is pressed or if an analog value is greater than some threshold. The conditional statement is one that is used often when using sensors with Arduino.

An if statement must have a test within the parentheses ( …. ) that can result in being true or false.  Most of the time you will also want to code a part that is executed when the statement is False. This can be done by using an else statement after the if statement.

You could use only an if statement in some cases but the if … else statement allows greater control over the flow of a code than the basic if statement.

An else clause (if at all exists) will be executed if the condition in the if statement is false.

You could use several if statements until a true test is encountered. Again, when the true test is found, the code written in that block will run. This is called an else …. if statement. You can have an unlimited amount of else….if conditions in a sketch.

The generic code for an If statement is:

if (condition) {
  // code executed if condition is true
}

How does the if statement works?

The if statement evaluates the test inside the parenthesis().

  • If the test is evaluated to be true, statements inside the brackets are executed.
  • If the test is evaluated to be false, statements inside the brackets are not executed.

 Conditions in an if statement:  the conditional part if the if statement includes the boolean expression, which can be true or false.

Inside the parentheses, you can use various operators. The comparisons that you can use are listed below.

  • x ! = y ( x not equal to y )
  • x < y ( x less than y )
  • x > y ( x greater than y )
  • x = = y ( x equal to y )
  • x < = y ( x less than or equal to y )
  • x > = y ( x greater than or equal to y )

As you can see above, the is not a single equal sign (x = y). The single equal sign is an assignment operator and sets x to y. It will put the value of variable y into the variable x. Instead, we use a double equal sign (x == 10), which is the comparison operator, and tests whether x is equal to y or not.

Example 1, if statement

/*
   Array sketch with an if statement
   Thee code has a counter that is tested
   in the if statement if it is true / false
*/

int Number1 = 10;
int Number2 = 15;
int Number3 = 20;

int Counter = 0;

void setup()
{
  Serial.begin(9600);

} // close void setup()

void loop()
{
  //printing the counter number
  Serial.println(Counter);
  // add a delay
  delay(1000);
  //using the compound operator
  Counter++;

  if (Number1 == Counter) {

    // Print the number of the counter
    Serial.print("The counter is now ");
    Serial.println(Counter);
    Counter = 0;
    delay(1000);

  } // closing if statement

} // closing void loop()

In the example above, we begin to define and initialize the variables that we will use. We will also use a counter that needs to be defined to understand how an if statement works.

//declare variable for the numbers
int Number1 = 10;
int Number2 = 15;
int Number3 = 20;

//declare a counter variable
int Counter = 0;

We initiate serial communication in the void setup() to allow communication with the serial monitor.

Serial.begin(9600);

At the beginning of the sketch, we print out the variable counter and add a small delay(). Every time the loop is repeated we add 1 at the variable counter and print the results.

/printing the counter number 
  Serial.println(Counter);
  //using the compound operator
  Counter++;

We arrived at the if statement, in our case, checks whether the variable Counter is equal to the variable Number 1. If that is the case, it will print a message to the serial monitor. After the if statement executes, the void loop() is repeated, and Counter will not be equal to Number1.

if (Number1 == Counter) {

    // Print the number of the counter
    Serial.print("The counter is now ");
    Serial.println(Counter);
    Counter = 0;
    delay(1000);

  } // closing if statement

If you want to do one thing if a statement is true and another if it is false, use the if…else statement:

if……else statement

Before we will dive into an example, let us first look at the generic code for and if …. else statement.

if (test condition) {
    // statements to be executed if the test 
   //expression is true
}
else {
    // statements to be executed if the test 
   //expression is false
}

In the example above, we begin to define and initialize the variables that we will use. We will also use a counter that needs to be defined to understand how an if statement works.

How does the if……else statement work?

The if…else statement works as follows when the condition is tested true:

  • statements inside the brackets of the if are executed when the condition is true
  • statements inside the brackets of the else are skipped

The if…else statement works as follows when the condition is tested false:

  • statements inside the brackets of if are skipped
  • statement inside the brackets of else are executed.

Example 2, if…else statement

/*
   Array sketch with if / else statement
   the code has a counter that executes a
   statement if a particular number is reached
*/

//declare variable for the numbers
int Number1 = 10;
int Number2 = 15;
int Number3 = 20;

//declare a counter variable
int Counter = 0;

void setup()
{
  // begin serial communication
  Serial.begin(9600);

} // close void setup()

void loop()
{
  //printing the counter number
  Serial.println(Counter);
  // add a delay
  delay(1000);
  //using the compound operator
  Counter++;
  // check whether if condition is true
  if (Number1 == Counter) {

    // Print the number of the counter
    Serial.print("The counter is now ");
    Serial.println(Counter);
    Counter = 0;
    delay(1000);

  } // closing if statement

  else {

    Serial.print("Numbers do not match ");
    //using the compound operator

  }

} // closing void loop()

In contract to the if statement we added a line of code with an else statement. This statement is executed when the condition in the if statement is false. Most of the code functions the same as in example 1. Only the else statement is added.

else{
  Serial.print("Numbers do not match");
  //using the compound operator
  Counter++;
}

There is another form that we can use in an if statement. That is the else … if statement. This allows testing multiple conditions in one test.

How does the else….if statement work?

The if…else statement works as follows when the condition is tested true:

  • statements inside the brackets of the if are executed when the condition is true
  • statements inside the brackets of the else are skipped

The if…else statement works as follows when the condition is tested false:

  • statements inside the brackets of if are skipped
  • statement inside the brackets of else are executed.

Example 3, else….if statement

/*
   Array sketch with if / else statement
   the code has a counter that executes a
   statement if a particular number is reached
*/

//declare variable for the numbers
int Number1 = 10;
int Number2 = 15;
int Number3 = 20;

//declare a counter variable
int Counter = 0;

void setup()
{
  // begin serial communication
  Serial.begin(9600);

} // close void setup()

void loop()
{

  //printing the counter number
  Serial.println(Counter);
  // add a delay
  delay(1000);
  //using the compound operator
  Counter++;

  // check whether if condition is true
  if (Number1 == Counter) {

    // Print the number of the counter
    Serial.print("The counter is now ");
    Serial.println(Counter);

    delay(1000);

  } // closing if statement

  else if (Counter == Number2) {

    // Print the number of the counter
    Serial.print("The counter is now ");
    Serial.println(Counter);

  } // closing else if statement

  else if (Counter > Number3) {

    Serial.println("Counter is  bigger than 20");
    Counter = 0;
  }

} // closing void loop()

In contract to the if statement we added a line of code with an else statement. This statement is executed when the condition in the if statement is false. Most of the code functions the same as in example 1. Only the else statement is added.

Example 4, nested if…else statement

You can also have an if statement within an if statement. It will be similar to the else…if statement but with two if statements.

/*
   Array sketch with if / else statement
   the code has a counter that executes a
   statement if a particular number is reached
*/

//declare variable for the numbers
int Number1 = 10;
int Number2 = 15;
int Number3 = 20;

//declare a counter variable
int Counter = 0;

void setup()
{
  // begin serial communication
  Serial.begin(9600);

} // close void setup()

void loop()
{
  //printing the counter number
  Serial.println(Counter);
  //using the compound operator
  Counter++;

  // check whether if condition is true
  if (Counter <= Number1) {


    if (Number1 == Counter) {
      // Print the number of the counter
      Serial.println("The counter is now ");
      Serial.print(Counter);

      delay(1000);
    }

    else {
      // Print message
      Serial.println("The counter is not equal to number1");
      delay(1000);
    }

  } // closing if statement

  else {
    Serial.println("Number1 is bigger than counter");
    delay(1000);
    Counter = 0;
    //using the compound operator

  }

} // closing void loop()

Overview if statements

I hope you learned something about the if statement. If there is any question, please free to ask them in the comments.

Previous Post
Create a For Loop statement in Arduino
Next Post
Understanding Arrays in Arduino
You must be logged in to post a comment.
Menu