LESSON 07 – IF STATEMENTS

IF STATEMENTS

 

An IF Statement is a programming structure that allows our programs to make decisions based on a condition.  Essentially, it allows a program do run some statements when a condition is true.  It can also allow for other statements to be ran when the condition is not true.

 

IF STRUCTURE

 

In general, the simplest IF Statement looks like this:

 

if (condition)

{

    //statement block

}

 

A statement block is simply one or more statements.  These statements are only executed when the condition is true.  If the condition is false, the Arduino skips to the statement following the closing bracket of the IF structure.

 

These statements should be indented inside the brackets.

 

CONDITIONS

 

A condition is a piece of code that can be evaluated to be either TRUE or FALSE.

 

CONDITIONAL OPERATORS

 

The conditional operators are

 

< 

Lesser than

<=

Lesser or Equal to

> 

Greater than

>=

Greater or Equal to

==

Equal to

!=

Not equal to

 

The above operators allow for a Boolean expression to be evaluated to TRUE or FALSE.

 

EXAMPLE

 

Consider the code below.

 

     if (voltage > 4)

     {

        //statement block

     }

 

EXAMPLE

Here is an example where a statement is executed if the variable called mark is greater than 80!

 

void setup()

{

   int mark = 85;     //change mark and try running again

   Serial.begin(9600);

 

   if (mark >= 80)

   {

       Serial.println("Wow, you are rocking this course!");

   }

}

 

Note: You can change the value of mark and re-execute the program in order to see how the execution changes.

 

 

If the variable called voltage is in fact greater than 4, then the condition is true and the statement block will be executed.  If the condition is false, the statement block is skipped.

 

ELSE

 

Sometimes, we want to run some statements when a condition is true but other statements when it is false.  We can do that by adding an else section to the code.

 

     if (condition)

     {

         //statement block used if condition is true

     }

     else

     {

        //statement block used if condition is false

     }

 

Note: No matter the condition’s value, Arduino will only execute one of the statement blocks.

 

EXAMPLE

Here is an example where the value given to the variable mark will impact which statement block will be executed.

 

void setup()

{

   int mark = 47;     //change mark and try running again

   Serial.begin(9600);

 

   if (mark >= 80)

   {

       Serial.println("You are passing!");

   }

   else

   {

       Serial.println("Uh oh!  You are failing!");

   }

}

 

ELSE IF

 

Sometimes, it is useful to create a structure that will have many possible situations that all have their own statement block.  We can do this using as many ELSE IF sections as we want.

 

Here is a general example with two else if sections.

 

     if (condition1)

     {

         //statement block used if condition1 is true

     }

     else if (condition2)

     {

         //statement block used if condition2 is true

     }

     else if (condition3)

     {

         //statement block used if condition3 is true

     }

     else

     {

        //statement block used if no condition is true

     }

 

EXAMPLE

 

In this example, we will output the grade mark associated with each number mark.

 

void setup()

{

   int mark = 47;     //change mark and try running again

   Serial.begin(9600);

 

   if (mark >= 80)

   {

       Serial.println("A");

   }

   else if (mark >= 70)

   {

       Serial.println("B");

   }

   else if (mark >= 60)

   {

       Serial.println("C");

   }

   else if (mark >= 50)

   {

       Serial.println("D");

   }

   else

   {

       Serial.println("F");

   }

}

 

COMPOUND BOOLEAN OPERATORS

 

We can combine several conditions using either AND or OR.  The symbol for AND is && and the symbol for OR is ||.

 

In general, using AND looks like this:

 

if(condition1 && condition2)

{

    //statements are executed if both conditions are true

}

 

In general, using OR looks like this:

 

if(condition1 || condition2)

{

    //statements are executed if one of the conditions

    //is true

}

 

EXAMPLE

 

In this similar example, we want to have a new message for when a mark is illegal.  That involves two possible conditions – when it is below 0 or above 100.   We will use an OR operator to combine the conditions into one.

 

void setup()

{

   int mark = 87;

   Serial.begin(9600);

 

   if (mark > 100 || mark < 0)

   {

       Serial.println("Illegal Value");

   }

   else if (mark >= 80)

   {

       Serial.println("A");

   }

   else if (mark >= 70)

   {

       Serial.println("B");

   }

   else if (mark >= 60)

   {

       Serial.println("C");

   }

   else if (mark >= 50)

   {

       Serial.println("D");

   }

   else

   {

       Serial.println("Failing");

   }

}

 

TRY THIS…

 

PRACTICE 07

 

Click here to go to the program analysis page.

 

PRACTICE 08

 

You will create a program that will continuously output the numbers from 1 to 100 over and over again.

 

HINTS:

 

The number will be a global variable.

 

The number is displayed and incremented inside the loop() function.  An if statement is used to check if the number has reached 100.  If it has, it is reset to 1.

 

PRACTICE 09

 

Similar to Practice program #6.  You will connect an LED and resitor to any digital pin that can do Pulse Width Modulation.

 

You will create a global variable called voltageNumber that keeps track of the voltage number which ranges from 0 to 255. 

 

Inside the loop() function, you will increase the voltageNumber by one and analogWrite() the new voltageNumber to the pin with the LED.  So the LED should gradually increase in brightness.

 

In order to slowdown the increases, you will put a short delay of 10 milliseconds.

 

Also, you will place your commands above inside of an if statement to make sure that voltageNumber never exceeds 255.

 

ALTERATIONS

 

Using the idea from above, try to make the LED fade in and then fade out continuously. 

 

Hint:  This is done in the Example calle Fade.