Lab Exercise 7: Loops

 Objective

The purpose of this lab is to illustrate the use of the various looping mechanisms available to us in C. We will work with the for loop, while loop and do…while loop in this exercise. When you complete this lab, you should be able to create iterative code that will test its entry and/or exit conditions at the appropriate point in the algorithm.

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, 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 Lab07.X folder.

Select Open Project OpenProjectButton.png.

2

Open C Source File

Lab7C.png

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

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

3

Edit Source File

In this lab, we have examples of the three loops, with an extra example of the do…while loop to illustrate the situation where only one pass will occur. To understand the program flow, see the flowchart below.

STEP 1:
Create a for loop to iterate through the block of code below. The loop should do the following:

  • Initialize counter1 to 1
  • Loop as long as counter1 is less than 5
  • Increment counter1 on each pass of the loop
  • (HINT: for (init, test, action)) { … }

STEP 2:
Create a while loop to iterate through the block of code below. The loop should run until charVariable1 is 0.
(HINT: while(condition)) { … }

STEP 3:
Create a do…while loop to iterate through the block of code below. The loop should run until counter1 is greater than 100.
(HINT: do { … } while(condition));

Lab7Flow.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.

Wait for the UART 1 Output window to finish printing.

Click on the Halt Debug_Pause.png button. This will stop execution so that we may examine the variables and their values.

 Results

5

End Debug Session

End the Simulation Session by clicking the Finish Debugger Session Debug_Finish_Debugger_Session.png button.

Clear out the UART 1 Output window (Ctrl + L)

Close the Project.

 Conclusions

C provides tremendous flexibility when it comes to loops. The for loop makes it possible to perform loop overhead tasks outside of the main body of the code block, by allowing variable initialization and modification within its parameter list. The while loop and do…while loop are just as flexible, but require more code in the body of the loop to perform the same actions. The while loop (and the for loop) checks its condition at the top of the loop, so if the condition isn't true at the beginning, it will never execute. In contrast, the do…while loop checks its condition at the end of the loop, so you will always have at least one loop iteration, even if the loop condition is not true from the start.

20th Annual
Microchip MASTERs Conference 2016
Register now - Deadline: July 29

JW Marriott Desert Ridge Resort-Phoenix, AZ

© 2016 Microchip Technology, Inc.
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.