Lab Exercise 7: Loops


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
Windows Linux Mac OSX
Integrated Development Environment
C Compiler

Exercise Files

File Download
Windows Linux Mac OSX
Project and Source Files



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.


Open C Source File


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


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.

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)) { … }

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

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));



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.



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.


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.

© 2017 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.