Professional Documents
Culture Documents
1. In the C programming language, a break statement can be used to immediately exit a for loop
or while loop. For example, take a look at the program below.
main()
{
int x = 0;
int alarm = 0;
while (x<10000)
{
x = x + 1;
if (alarm == 4)
{
break;
}
}
while(1);
}
In this program, we first create two variables, x and alarm, and assign them a value of 0.
Next, we reach our while loop. In each iteration of the loop, we will increment the control
variable, x, and test alarm. As long as alarm is not equal to 4, the while loop will work as
before, counting all the way to 10,000. The program will then continue to the infinite
while(1); loop.
However, if alarm is ever equal to 4, the program will reach the break statement. This causes
the program to immediately cease the while loop and the program will immediately jump to the
next instruction – or in this case – the while(1); loop.
2. Create a new CCS project called Break. Copy the program from above into the project’s main.c
file.
5. Open the Debugger. Make sure you can see the variables. Make sure the variables’ Number
Format is Decimal.
6. Step Into your code 10-15 times. The variables x and alarm will be set to 0 and then x will
increment as the while loop runs.
Left-click on the value of the alarm variable. This will allow you to edit its value.
Left click
8. Enter a value of 4 for the variable and press the [Enter] key.
10. As soon as you press the Step Into button one more time, the program immediately moves to
the while(1); statement.
12. Looking back at the program in the CCS Editor, we can see that to force the program to reach
the break statement, we would need to manually force alarm to be equal to 4. There are no
instructions in the program that do this for us.
This happens often in a program with inputs from the outside world.
Take a look at the program below. Can you figure out what will happen? (Don’t worry, we will
tell you on the next page, but try to figure it out for yourself for a moment.)
#include <msp430.h>
main()
{
WDTCTL = DEVELOPMENT; // Need for development mode
while(1) // ???
{
if( (BUTTON11 & P1IN) == 0 ) // ???
{
P1OUT = BUTTON11; // ???
break;
}
while(2);
}
It begins by disabling the watchdog protection circuitry and enabling the input/output pins to use
the P1.1 push-button switch and the red LED. It also turns the red LED on.
The program then enters the while(1) loop. Normally, we would think of as an infinite loop,
but the break statement will allow us to exit the loop in a little bit.
In each iteration of the while(1) loop, the program will test to see if the P1.1 button is pushed.
(You may want to refresh your memory on this instruction with the previous handouts if this is
still a little confusing for you. If you are still struggling with it, please let us know. We’re here to
help!)
However, if the button is pushed, the program will turn off the red LED and then “break” out of
the while(1) loop where it stays forever.
#include <msp430.h>
main()
{
WDTCTL = DEVELOPMENT; // Need for development mode
while(2);
}
Try both running the program and single-stepping through the program a couple times until you
are comfortable with the use of the break statement.
When you are ready, Terminate the Debugger to return to the CCS Editor.
continue is similar to a break statement, but you can think of it as a slightly weaker version.
When a program reaches a continue statement inside of a for or while loop, the
microcontroller will skip the rest of the instructions in the existing loop iteration. All of the
instructions that are located between the continue statement and the end of the loop will not be
executed. The program will then immediately jump to the next loop iteration.
This is in direct contrast with the break statement. break will cause a program to completely
exit a loop, but continue will skip the rest of the current loop iteration and move to the next
iteration.
#include <msp430.h>
main()
{
long x = 0; //
x = x+1; //
//
}
Then the program enters a while(x<200000) loop. In each iteration, the program checks to see
if the button is pushed. If the button is not pushed, the program continues to increment the value
of x. Therefore, if the button is not pushed, the while loop will continue to run until x is
incremented to 200000 (or approximately 5 seconds). After x reaches 200000, the loop ends, the
red LED is turned off, and the program enters the while(1); infinite loop.
However, if the button is pushed, the program will hit a continue statement. This causes the
present iteration to end and return to the top of the while(x<200000) loop without
incrementing x. Therefore, anytime that the button is pressed will not count toward the 5 second
count.
#include <msp430.h>
main()
{
long x = 0; // Used to create a 5 second counter
20. Run the program a couple of times. The first time, just let the program run without pushing the
button to verify that the LED comes on for about 5 seconds before turning off.
Next, click Suspend (pause) and then click on Soft Reset to enable the program to start over.
Run the program a second time, but this time, push and hold the P1.1 push-button down for 15
seconds. The LED will stay on the entire time that the button is pressed, and will turn off shortly
after you release the button.
Click Suspend (pause) and Soft Reset to enable the program to start over and try it a couple
more times.
You can also try single-stepping through the program with the button pushed and not pushed to
see how the variable x changes.
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.