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 an iterative code that tests 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 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 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 builds and sends 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 stops execution so that we may examine the variables and their values.


After building, executing, and stopping your code, the UART1 Output window should resemble the screenshot below, where the relevant variables are printed out in each iteration of the respective loop. Note that in the very last do…while loop (you didn't write code for this one), it was set up such that the condition was not met when the loop started, but since the condition check doesn't occur until the end of a loop iteration, the loop did execute one time. This is why the last line in the output window says “DO1: counter2 = 316”.



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.


Line numbers correspond to those in the provided solution file.

Lines 55-59
There are three parts to the creation of a for loop:

for( counter1 = 1 ; counter1 < 5 ; counter1++)
intVariable1 *= counter1;
printf("FOR: intVariable1 = %d, counter1 = %d\n", intVariable1, counter1);

The first part is to initialize the loop count variable. This is done as the first for loop parameter statement. Next, we need to specify the condition under which the loop will continue looping. This is the second parameter. Remember, the loop will continue as long as this statement is true, and it is tested at the top of each loop iteration. Finally, we need to specify some action to take for each iteration of the loop. This is the third parameter where we increment the variable counter1. So, the code between the curly braces repeats until counter1 >= 5. During each iteration of the loop, the variable counter1 is incremented.

Lines 70-75
Step 2 creates a while loop. This is a loop where the condition is tested at the top of the loop, so if the condition is not met, the loop will never execute. A while loop is similar to a for loop, and in fact it is a special case of a for loop (equivalent to for ( ; condition; ) {…}). The only thing you specify in a while loop is the exit condition. Any loop counting will have to be conducted manually within the loop itself.

while( charVariable1 != 0)
charVariable2 += 5;
printf("WHILE: charVariable1 = %d, charVariable2 = %d\n", charVariable1, charVariable2);

Like the for loop, the while loop continues to execute as long as the condition is true. Since we want to execute this code until charVariable1 is 0, we simply specify a condition of charVariable1 not equal to 0.

Lines 89-94
Step 3 creates a do…while loop. This is similar in concept to the while loop, in that the only thing we specify is the condition. However, the do…while loop checks its condition at the end of a loop iteration. Therefore, it is possible to execute the loop once, even if the condition is false.

counter1 += 5;
counter2 = counter1 * 3;
printf("DO: counter1 = %d, counter2 = %d\n", counter1, counter2);
} while(counter1 <= 100);

The do…while loop starts with the do keyword. Since we are executing multiple statements in the loop, we must enclose them in curly braces. Finally, the do…while loop ends with the keyword while followed by the loop condition. In this case, the code continues looping until counter1 is greater than 100. Depending on how we increment counter1, it is possible for it to be greater than 100 on its final iteration.


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 never executes. 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.

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