Lab Exercise 5: if Statements

 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.

Software Tools

Tool About Installers
Installation
Instructions
Windows Linux Mac OSX
MPLAB® X
Integrated Development Environment
MPLAB® XC16
C Compiler

Exercise Files

File Download
Installation
Instructions
Windows Linux Mac OSX
Project and Source Files

 Procedure

1

Open the Project

Start MPLAB® X IDE, then click on the Open Project Main_Open_Project.png 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 Project OpenProjectButton.png.

2

Open C Source File

Lab05C.png

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

3

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:

FlowChart.png

4

Debug Project

Once you finish writing the code:

Click on the Debug Project Main_Debug_Project.png button. This will build and send the program to the simulator.
Click on the Continue Debug_Continue.png button. This begins the simulation.
Click on the Halt Debug_Pause.png 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


5

End Debug Session

End the Simulation Session by clicking the Finish Debugger Session Debug_Finish_Debugger_Session.png 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.

© 2024 Microchip Technology, Inc.
Notice: ARM and Cortex are the registered trademarks of ARM Limited in the EU and other countries.
Information contained on this site regarding device applications and the like is provided only for your convenience and may be superseded by updates. It is your responsibility to ensure that your application meets with your specifications. MICROCHIP MAKES NO REPRESENTATIONS OR WARRANTIES OF ANY KIND WHETHER EXPRESS OR IMPLIED, WRITTEN OR ORAL, STATUTORY OR OTHERWISE, RELATED TO THE INFORMATION, INCLUDING BUT NOT LIMITED TO ITS CONDITION, QUALITY, PERFORMANCE, MERCHANTABILITY OR FITNESS FOR PURPOSE. Microchip disclaims all liability arising from this information and its use. Use of Microchip devices in life support and/or safety applications is entirely at the buyer's risk, and the buyer agrees to defend, indemnify and hold harmless Microchip from any and all damages, claims, suits, or expenses resulting from such use. No licenses are conveyed, implicitly or otherwise, under any Microchip intellectual property rights.