Last Updated: 20/07/2021

Lesson 3 - if else

Lessons >> Basics >> Lesson 3 - if else

Lesson Contents

Using "if" to control the flow of code
Using different Comparison Operators == (equals), != (not equal), < (smaller than), > (greater than), <= (smaller or equal) >= (greater or equal)
Using multiple "if"controls
Using "if else" to control the flow of code
Nesting if "if" and "if else" controls
Variable naming.
Understanding the int Variable and it's size limits.

Example 1: ifelsev1.ino


Click to Download code:ifelsev1.ino

Using "if" to control code

 
/* 19/07/2021
   ifelsev1

   Purpose, to to learn to use the "if" function


*/

//choosing a name
int q;
//start with a lower case letter, if a second word no spaces butu start with a capital letter
//Example below
int myNumber;
int myDistance;

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

}

void loop() {
  // print in the loop
  Serial.print("Val q: ");
  Serial.println(q);
  // ">" means greater than
  /*
  if(q > 10){//change the value from 10 to 20
    q = 0;
  }
  */
  if(q > 20){//change the value from 10 to 20
    q = 0;
  }
  q++;
  delay(100);
}

Example 2: ifelsev2.ino


Click to Download code:ifelsev2.ino

Multiple "if" controls

 
/* 19/07/2021
   ifelsev2

   Purpose, to to learn to use the "if" function


*/

//choosing a name
int q;
//start with a lower case letter, if a second word no spaces butu start with a capital letter


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

}

void loop() {
  // print in the loop
  Serial.print("Val q: ");
  Serial.println(q);
  //equals to IMPORTANT NOTE q == 4 NOT the same as q = 4;
  if(q == 4){
    Serial.println("q == 4");
  }
  //q NOT equal to 4
  if(q != 4){
    Serial.println("q != 4");  
  }
  //q smaller than 4
  if(q < 4){
    Serial.println("q < 4 smaller than");
  }
  //q smaller or equal to 4
  if(q <= 4){
    Serial.println("q <= 4 smaller or equal");  
  }
  //q greater or equal to 4
  if(q >= 4){
    Serial.println("q >= 4 greater or equal");  
  }
  
  // ">" means greater than
  if(q > 10){//change the value from 10 to 20
    q = 0;
  }
  
  
  q++;
  delay(100);
}

Example 3: ifelsev3.ino


Click to Download code:ifelsev3.ino

Using "if else"

 
/* 19/07/2021
   ifelsev3

   Purpose, to to learn to use the "if else" function


*/

//An int is an integer, a Number, any value between -32,768 and 32,767
int q;
//start with a lower case letter, if a second word no spaces but start with a capital letter


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

}

void loop() {
  // print in the loop
  Serial.print("Val q: ");
  Serial.println(q);
  //equals to IMPORTANT NOTE q == 4 NOT the same as q = 4;
  if (q >= 4) {
    Serial.println("q >= 4");
    if (q == 5) {
      Serial.println("q == 5");
    }
  } else {
    if (q != 4) {
      Serial.println("q != 4");
    }
    if (q < 4) {
      Serial.println("q < 4 smaller than");
    }
  }
  /*
    //q NOT equal to 4
    if(q != 4){
    Serial.println("q != 4");
    }
    //q smaller than 4
    if(q < 4){
    Serial.println("q < 4 smaller than");
    }
    //q smaller or equal to 4
    if(q <= 4){
    Serial.println("q <= 4 smaller or equal");
    }
    //q greater or equal to 4
    if(q >= 4){
    Serial.println("q >= 4 greater or equal");
    }
  */

  // ">" means greater than
  if (q > 10) { //change the value from 10 to 20
    q = 0;
  }


  q++;
  delay(100);
}

Example 4: ifelsev4.ino


Click to Download code:ifelsev4.ino

Nested "if else"

 
/* 19/07/2021
   ifelsev4

   Purpose, to to learn nested if else


*/

//An int is an integer, a Number, any value between -32,768 and 32,767
int q;
//start with a lower case letter, if a second word no spaces but start with a capital letter


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

}

void loop() {
  // print in the loop
  Serial.print("Val q: ");
  Serial.println(q);
  
  if (q >= 4) {
    
    if (q == 5) {
      Serial.println("q == 5");
    }else{
      Serial.println("q >= 4"); 
    }
  } else {
    if (q != 4) {
      Serial.println("q != 4");
    }
    if (q < 4) {
      Serial.println("q < 4 smaller than");
    }
  }
  

  // ">" means greater than
  if (q > 10) { //change the value from 10 to 20
    q = 0;
  }


  q++;
  delay(100);
}

Additional Resource Links

Don't forget to use the Reference in you Arduino IDE and look at the Control Structures if else 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 3 - if else as a reference.