Lab Exercise 5: if Statements

Last modified by Microchip on 2023/11/09 09:21

Objective

The purpose of this lab is to illustrate the use of the if statement to make decisions in code. An if statement will execute a block of code if some specified condition is met. The goal of this lab is for you to become comfortable with the if statement syntax and how you create the condition expressions.

Reference Materials

Exercise Files

Procedure

Open the Project

Start MPLAB® X IDE, then click on the Open Project openproject icon on the main toolbar

Navigate to the folder where you saved the exercise files for this class.

Click on the Lab05.X folder.

Select Open ProjectOpen Project Button

Open C Source File

open the lab 5 source file

Open the Lab05.c source file from the project tree by double-clicking on its icon.

This will bring up Lab05.c in a window to edit

Edit Source File

Search the code for lines with the comment “//### Your Code Here ###”. There will be additional comments above these lines with complete instructions, and in some cases, there will be comments to the right with specific details regarding a particular line of code.
Note that comments with instructions for your tasks are surrounded by ‘#’ to make them easy to spot.

STEP 1:
Increment intVariable1 if BOTH the following conditions are true:

floatVariable2 is greater than or equal to floatVariable1
charVariable2 is greater than or equal to charVariable1

Remember to use parentheses to group operations. This step will require you to use logical operators such as ‘&’ and relational operators such as ‘>=‘.

STEP 2:
If the above is not true and floatVariable1 is greater than 50 then decrement intVariable1 (Hint: else if).

STEP 3:
If neither of the above is true, set charVariable2 equal to 1. (Hint: else)

This code you are writing implements the flowchart below:

Flow Chart for the lab

 

Debug Project

Once you finish writing the code:

Click on the Debug Project Main Debug Project button. This will build and send the program to the simulator.
Click on the Continue Debug Continue button button. This begins the simulation.
Click on the Halt Debug Pause button button. This will stop execution so that we may examine the variables and their values. Open the Variables Window with either Window > Debugging > Variables or ( Alt + Shift + 1)

Results

results in the output window

After you build the code, run it and then stop it, you should see these results in your Variables window.

Next, we will cover the code step by step.

Code Analysis

(NOTE: Line numbers correspond to those in the provided solution file.)

Lines 33-38
As in previous labs, we have created several variables that we will manipulate to illustrate the main idea of the lab.
char charVariable1 = 50;
char charVariable2 = 100;
int intVariable1 = 1000;
int intVariable2 = 10000;
long longVariable1 = 1000;
long longVariable2 = 2000;
float floatVariable1 = 34.5678;
float floatVariable2 = 156.78956;

Lines 51-54
STEP 1:
This code will increment intVariable1 if both of the two specified conditions are true. This can easily be implemented as an if statement, with two conditions connected by the logical AND operator.

if((floatVariable2 >= floatVariable1) && (charVariable2 >= charVariable1))
{
intVariable1++;
}

In order for intVariable1 to be incremented, both of the conditions must be true: floatVariable2 must be greater than or equal to floatVariable1, and charVariable2 must be greater than or equal to charVariable1. Note that the operator connecting the two conditions is the logical AND ‘&&’. Make sure you use the double ampersand, otherwise, it will not function properly in all conditions.

Lines 61-64
STEP 2:
This code will decrement intVariable2 if the previous condition (STEP 1) was not true. This can most easily be accomplished by using an else if statement connected to the if statement above.

else if(floatVariable1 > 50)
{
intVariable2--;
}

Since this else if statement follows the if statement above, it will check its condition only if the one above is false. So, for intVariable2 to get decremented the following conditions must be met: floatVariable2 must be less than floatVariable1, and charVariable2 must be less than charVariable1, and floatVariable1 must be greater than 50.

Lines 71-74
STEP 3:
This step will set charVariable2 equal to 1 if all the other conditions were false. This can be done by using an else statement after the if and else if statements above.

else
{
charVariable2 = 1;
}

At this point, charVariable2 will only be set equal to 1 if floatVariable2 is less than floatVariable1 and charVariable2 is less than charVariable1 and floatVariable1 is less than or equal to 50.

End Debug Session

End the Simulation Session by clicking the Finish Debugger Session Debug Finish Debugger Session button button. Close the Project.

Conclusions

The if statement makes it possible to execute blocks of code only when some specified set of conditions are met. When used in conjunction with the else if and else statements (which can only be used following an if statement), it is possible to drill down through several levels of conditions and execute a different block of code for each level.