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

Using Switch Statements in Arduino

in Arduino Basics

In the if statement tutorial, we explained how you could control a program’s flow by testing conditions in your code. So, Like if statements, switch statements control the program’s flow by allowing the programmer to specify the code that should be executed in various conditions. When a case statement is found whose value matches that of the variable, the code in that statement is executed.

After the statement is executed, we write the keyword “break” at the end of each case. The break statement tells the Arduino to stop the current switch statement and move on to another part of the program.

To clarify how this is done, we will use an Arduino Board and Serial communication to provide examples of how to use a switch statement in an Arduino Sketch.

Expressions, data-types, in switch statements must have an integral or unenumerated type such as integer and char. You cannot use a switch statement for a string of characters (array of characters). If you would like to use a string of characters in a conditional statement, you should use the IF statement. It is possible to convert a string to be used in a switch statement, but that involves some harder programming that is not suitable for this tutorial’s purpose.

Generic code switch statement

switch (variable) {
case label1:
  // statements
  break;
case label2:
  // statements
  break;
default:
  // statements
  break;
}

The variable is evaluated and compared with the values of each case.

If there is a match, the statements after the matching label are executed until the break is encountered.

If there is no match, the default statements are executed.

The break statement is an important one since it tells the program to end the statement. If you do not use a break statement, all statements after the matching label are executed.

The syntax default can be used to execute code if no value of the cases matches the expression value.

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 equal ");


      delay(1000);
    }

    else {
      // Print message
      Serial.println("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
Understanding Arrays in Arduino
Next Post
Understanding character strings in arduino

3 Comments. Leave new

Jacquelynn Karlsen
8 April 2020 19:09

Remarkable! Its truly remarkable piece of writing, I have got much clear idea on the topic of from this paragraph.

Earn Online
12 October 2020 13:54

I’m really impressed along with your writing talents and also with the format in your weblog. Is this a paid topic or did you modify it yourself? Anyway stay up the nice high quality writing, it’s uncommon to look a great weblog like this one nowadays.

You must be logged in to post a comment.
Menu