Last Updated: 20/07/2021

Lesson 5 - switch

Lessons >> Basics >> Lesson 5 - switch

Lesson Contents

Using "switch" to control program flow
Learning the "switch" syntax
Comparing a single value in a "case"
Comparing multiple values in a "case"
Understanding the "default "
Understanding "break"
Nesting a "switch" statement in a "for loop"
Nesting a "for loop" in a "switch" statement
Swapping a value between two variables
Using a variable to change the start value of a "for loop"

Example 1: switchv1.ino


Click to Download code:switchv1.ino

Simple introduction to the "switch" staement
Comparinf single value cases.
Using the" default"
break;

 
/* 21/07/2021
   switchv1

   Basic use of the switch statement


*/

//An int is an integer, a Number, any value between -32,768 and 32,767
int q;
int w;



void setup() {
  // Serial.begin(9600) starts serial communication. 9600bps bits per second = 1200 characters per second
  Serial.begin(9600);
  //Send script name
  Serial.println("switchv1...");
  //this will print a blank line
  Serial.println(" ");

}

void loop() {

  
  //q starts with a value of Zero, the loop continues while q smaller than 20
  for (q = 0; q < 20; q++) {
    Serial.print("q: ");
    Serial.println(q);
    switch(q){
      //q is equal to 1
      case 1:
        Serial.println("q is equal to 1");
        break;
        //q is equal to 2
      case 2:
        Serial.println("q == 2");
        break;
        //q is equal to a value we have not tested for
      default:
        Serial.println("q is not equal to 1 or 2");
        break;
    }
    //the following if statements are all covered by the switch statement above
    /*
    if(q == 1){
      
    }
    if(q == 2){
      
    }
    if(q > 2){
      
    }
    if(q<1){
      
    }
    */

  }
  //delay outside the for loop
  delay(100);

}

Example 2: switchv2.ino


Click to Download code:switchv2.ino

Using "case" to compare a range of values.

 
/* 21/07/2021
   switchv2

   Purpose, switch statement comparing multiple values in a single case


*/

//An int is an integer, a Number, any value between -32,768 and 32,767
int q;
int w;



void setup() {
  // Serial.begin(9600) starts serial communication. 9600bps bits per second = 1200 characters per second
  Serial.begin(9600);
  //Send script name
  Serial.println("switchv2...");
  //this will print a blank line
  Serial.println(" ");

}

void loop() {

  
  //q starts with a value of Zero, the loop continues while q smaller than 20
  for (q = 0; q < 20; q++) {
    Serial.print("q: ");
    Serial.println(q);
    switch(q){
      case 1:
        Serial.println("q is equal to 1");
        break;
      case 2:
        Serial.println("q == 2");
        break;
      case 5 ... 10:
        Serial.println("value from 5 to 10");
        break;
      default:
        Serial.println("q is not equal to 1 or 2");
        break;
    }
    

  }
  //delay outside the for loop
  delay(100);

}

Example 3: switchv3.ino


Click to Download code:switchv3.ino

Nested "switch" with "for loop"
Swapping a value between variables
Starting a for loop with a variable value.

 
/* 21/07/2021
   switchv3

   Purpose, nesting a for loop in a switch statement 


*/

//An int is an integer, a Number, any value between -32,768 and 32,767
int q;
int w;



void setup() {
  // Serial.begin(9600) starts serial communication. 9600bps bits per second = 1200 characters per second
  Serial.begin(9600);
  //Send script name
  Serial.println("switchv3...");
  //this will print a blank line
  Serial.println(" ");

}

void loop() {

  
  //q starts with a value of Zero, the loop continues while q smaller than 20
  for (q = 0; q < 20; q++) {
    Serial.print("q: ");
    Serial.println(q);
    switch(q){
      case 1:
        Serial.println("q is equal to 1");
        break;
      case 2:
        Serial.println("q == 2");
        break;
      case 5 ... 10:
        Serial.println("value from 5 to 10");
        //for(w=0;w<15;w++)...normal for loop
        //w takes on the value of q instead of 0 in the line above
        //this allows us to start our "for loop" at varying start values.
        for(w = q;w<15;w++){
          Serial.print("w: ");
          Serial.println(w);
        }
        break;
      default:
        Serial.println("q is not equal to any tested case");
        break;
    }
    

  }
  //delay outside the for loop
  delay(100);

}

Additional Resource Links

Don't forget to use the Reference in you Arduino IDE and look at switchl Structures and Comparison Operators for more help on this lesson.

Comments


This site has been designed to be child friendly, this means that comments cannot be added to videos or directly to the site.
To add a comment or ask a question please email the address in this image: and use Lesson 5 - switch as a reference.