Professional Documents
Culture Documents
01 - What Is A For Loop - MMB - v009
01 - What Is A For Loop - MMB - v009
01 - What Is A For Loop - MMB - v009
1. Let’s review the basics of a for loop. A for loop executes a block of code for a set number of
iterations and is formatted as follows:
control initializes a variable that will be used to iterate through the loop.
Finally, update will update the control variable after every iteration by performing some type of
operation on it.
2. Here is a more specific example of a program with a for loop that iterates 10 times.
As the loop runs, the variable, x, is added to another variable during each iteration. Therefore, we
would expect y would have a value of:
y = 0 + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 = 45
main()
{
int x, y; // Create two variables, x and y
y = 0; // Set y to be 0
3. First, this program creates two variables, x and y, and assigns the value 0 to variable y.
condition is then set to x<10. This means that the block of code inside of the for loop’s
brackets will evaluate over and over again (iterate) as long as x is less than 10. When x is not
longer less than 10, the for loop will stop and the program will move on to the next line of code.
update has been set to x=x+1. Therefore, if the condition is true, after the code inside of the
curly braces is executed, x will be incremented by 1 before the for loop tests the condition again.
5. After the for loop ends, the program comes to a while(1); infinite loop. Effectively, this
stops the program from executing any more instructions, but we will learn more about while
loops in an upcoming lesson.
6. Create a new CCS project by selecting New / CCS Project from the File menu.
Also, make sure you select Empty Project (with main.c) from the
Project templates and examples pane before clicking Finish.
10. In the Project Explorer pane, right click on your project name and select Properties from
the pop-up menu.
12. On the right side of the window, for the Optimization level, select off.
We just told CCS that we did not want its help during the Build process. Like a lot of other
software programs out there, CCS has some wonderful features to help expert users, but for now,
we are going to stick with just the basics.
14. When you are ready, go ahead and click OK. This will take you back to the CCS Editor.
15. Build your project. If you have any errors, make sure you did not accidentally modify your
program.
16. After successfully Building your project, launch the CCS Debugger.
Launching the Debugger can take a few moments. Do not forget, in addition to opening the
Debugger portion of CCS, the process is automatically programming your microcontroller, too.
18. If you see an error message like this, it probably means that you forgot to plug-in your Launchpad
board. Connect your Launchpad board to your PC with the USB cable and click Retry.
20. If your Variables pane is not open, or if you accidentally close it, it is easy to fix. Just select
Variables from the View menu.
In my example below, the highlighted light in the program window is at the top of the program.
22. Click the Step Into button. In my example below, the y=0 instruction is now highlighted.
Recall, the highlighted instruction is the next to be performed. There is also a small, blue arrow
at the highlighted line to indicate the same thing.
24. The next line to be performed will be to start the for loop. Click Step Into again.
26. Second, because x=0 is less than 10, you have entered the for loop and are prepared to do the
next instruction.
First, the value of x (presently 0) was added to the value of y (also 0). The result is stored in the
variable y.
Second, you reached the end of the first loop iteration. Therefore, the loop has returned to the top
of the for loop.
Second, the variable was tested by the condition, x<10. Since the condition is still true, the
program goes back into the loop to perform next.
30. Click Step Into again. x has been updated to 2. We are now ready to add the updated value of
x to the variable y.
32. Continue clicking Step Into until you reach this point in the program.
If you continue clicking Step Into at this time, the program will stay in the infinite loop.
35. If you want to do this again, or at any time start over, click the Soft Reset icon.
#include <msp430.h>
main()
{
WDTCTL = DEVELOPMENT; // Stop the watchdog timer
WDTCTL = DEVELOPMENT;
39. The next three instructions prepare the microcontroller to use one of its pins to turn on and off the
red LED. We will learn more about these instructions a little bit later.
40. Next, we create the variable x that will be used in the for loop.
Note, this only creates a variable called x. No value has been assigned to it yet. We say it has
not yet been initialized. Its value will be a random number that we cannot predict.
Anytime, you create a variable like this, you need to make sure you initialize it to a known value
before it is used. We will take care of that in the next line of the program.
41. We then come to the for loop. This loop will initialize x to be zero, and will increment x after
each loop iteration as long as x is less than 30000.
If the red LED was off, it will turn the light on.
If the red LED was on, it will turn the light off.
After the loop is complete, the program ensures that the red LED is turned off.
43. Finally, the program enters an infinite loop to prevent anything else from happening.
44. In the CCS Editor, delete all of the text inside your main.c file.
45. Paste the new program into your main.c file. Save and Build it.
47. Click the Resume button to run your program. The red LED at the bottom of the board should
turn on for about one second and then turn off.
49. Right now, your program is still running, but it is stuck in the infinite loop.
50. Now, click the Soft Reset button again to prepare your microcontroller to run again.
51. Try clicking Step Into 15-20 times and watch the board and Variables pane change as you step
through the code.
52. When you are ready, click Terminate to leave the Debugger and return to the Editor.
Here, you are going to turn on the red LED before the loops starts, and you are going to do
nothing in the for loop. The for loop will simply increment x to 30,000 creating a delay.
#include <msp430.h>
main()
{
WDTCTL = DEVELOPMENT; // Stop the watchdog timer
54. Modify your program. Save and Build it. When you are ready, launch the Debugger.
55. Click Resume to run your program. Again, the light on your board lights up and turns off after
approximately 1 second. However, since we eliminated everything inside of the loop, the LED
remained on the entire time instead of toggling on and off.
57. In some programs, you may see empty for loops written differently.
Here, we have:
58. This can be rewritten as this – without the curly braces, but including a semicolon at the end of
the for loop.
59. You can Save, Build, and Debug your program to verify this method also works.
In no event shall the owner or contributors to the tutorials and software be liable
for any direct, indirect, incidental, special, exemplary, or consequential damages
(including, but not limited to, procurement of substitute goods or services; loss of
use, data, or profits; or business interruption) however caused and on any theory
of liability, whether in contract, strict liability, or tort (including negligence or
otherwise) arising in any way out of the use of this software, even if advised of
the possibility of such damage.