You are on page 1of 11

What Are the Break and Continue Statements?

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.

3. In the project’s Properties, turn off the Optimization.

Copyright © 2012-2015 Page 1 of 11


Valparaiso University
4. Save and Build your program.

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.

Copyright © 2012-2015 Page 2 of 11


Valparaiso University
7. Since our program does not actually change the alarm value, the program will never reach the
break statement and cease the first while loop.

Therefore, we are going to manually change the alarm variables’ value.

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.

Copyright © 2012-2015 Page 3 of 11


Valparaiso University
9. Step Into your program again. This time, watch carefully when the program reaches the if
statement.

The next time you click


Step Into

10. As soon as you press the Step Into button one more time, the program immediately moves to
the while(1); statement.

The break statement causes the


program to leave the loop

Copyright © 2012-2015 Page 4 of 11


Valparaiso University
11. Go ahead and Terminate the Debugger when you are ready.

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>

#define RED_LED 0x0001 // P1.0 is the Red LED


#define BUTTON11 0x0002 // P1.1 is the button
#define DEVELOPMENT 0x5A80 // Stop the watchdog timer
#define ENABLE_PINS 0xFFFE // Required to use inputs and outputs

main()
{
WDTCTL = DEVELOPMENT; // Need for development mode

PM5CTL0 = ENABLE_PINS; // Prepare pins for I/O usage

P1DIR = RED_LED; // Pin connected to red LED


// will be an output

P1OUT = BUTTON11 | RED_LED; // Turn on red LED and the


P1REN = BUTTON11; // button needs a pull-up resistor

while(1) // ???
{
if( (BUTTON11 & P1IN) == 0 ) // ???
{
P1OUT = BUTTON11; // ???
break;
}

while(2);
}

Copyright © 2012-2015 Page 5 of 11


Valparaiso University
13. The program is very similar to the first program in this section.

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

If the button is not pushed, the while(1) loop simply repeats.

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>

#define RED_LED 0x0001 // P1.0 is the Red LED


#define BUTTON11 0x0002 // P1.1 is the button
#define DEVELOPMENT 0x5A80 // Stop the watchdog timer
#define ENABLE_PINS 0xFFFE // Required to use inputs and outputs

main()
{
WDTCTL = DEVELOPMENT; // Need for development mode

PM5CTL0 = ENABLE_PINS; // Prepare pins for I/O usage

P1DIR = RED_LED; // Pin connected to red LED


// will be an output

P1OUT = BUTTON11 | RED_LED; // Turn on red LED and the


P1REN = BUTTON11; // button needs a pull-up resistor

while(1) // Keep looping forever


{
if( (BUTTON11 & P1IN) == 0 ) // Is P11 button pushed?
{
P1OUT = BUTTON11; // Turn off the Red LED
break;
}

while(2);
}

Copyright © 2012-2015 Page 6 of 11


Valparaiso University
14. Try copying the new program into your CCS project. Save, Build, and launch the Debugger.

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.

15. Next, we will look at the continue statement.

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.

Copyright © 2012-2015 Page 7 of 11


Valparaiso University
16. Take a look at the program below. Can you figure out what it will do? (Again, we will tell you
on the next page.)

#include <msp430.h>

#define RED_LED 0x0001 // P1.0 is the Red LED


#define BUTTON11 0x0002 // P1.1 is the button
#define DEVELOPMENT 0x5A80 // Stop the watchdog timer
#define ENABLE_PINS 0xFFFE // Required to use inputs and outputs

main()
{
long x = 0; //

WDTCTL = DEVELOPMENT; // Need for development mode

PM5CTL0 = ENABLE_PINS; // Prepare pins for I/O usage

P1DIR = RED_LED; // Pin connected to red LED


// will be an output

P1OUT = BUTTON11 | RED_LED; // Turn on red LED and the


P1REN = BUTTON11; // button needs a pull-up resistor

while(x < 200000) //


{
if( (BUTTON11 & P1IN) == 0 ) // If P11 button pushed
{
continue; //
}

x = x+1; //
//
}

P1OUT = BUTTON11; // Turn off the red LED


while(1); // and stay here forever
}

Copyright © 2012-2015 Page 8 of 11


Valparaiso University
17. The program begins similar to the previous example. After creating a new counting variable, x,
the program disables the watchdog protection circuitry and enables the input/output pins to use
the P1.1 push-button switch and the red LED. It also turns the red LED on. (Note, we need to
use a long variable type for x because its maximum value will exceed that which can be stored
in an int variable.)

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>

#define RED_LED 0x0001 // P1.0 is the Red LED


#define BUTTON11 0x0002 // P1.1 is the button
#define DEVELOPMENT 0x5A80 // Stop the watchdog timer
#define ENABLE_PINS 0xFFFE // Required to use inputs and outputs

main()
{
long x = 0; // Used to create a 5 second counter

WDTCTL = DEVELOPMENT; // Need for development mode

PM5CTL0 = ENABLE_PINS; // Prepare pins for I/O usage

P1DIR = RED_LED; // Pin connected to red LED


// will be an output

P1OUT = BUTTON11 | RED_LED; // Turn on red LED and the


P1REN = BUTTON11; // button needs a pull-up resistor

while(x < 200000) // For about 5 seconds


{
if( (BUTTON11 & P1IN) == 0 ) // If P11 button pushed
{
continue; // Do not increment 5 second counter
}

x = x+1; // If P11 button was not pushed,


// increment the 5 second counter
}

P1OUT = BUTTON11; // Turn off the red LED


while(1); // and stay here forever
}
Copyright © 2012-2015 Page 9 of 11
Valparaiso University
18. Copy the above program into a new CCS Project called Continue. Make sure
Optimization is turned off in the Project Properties.

19. Save and Build your program. Launch the Debugger.

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.

Copyright © 2012-2015 Page 10 of 11


Valparaiso University
All tutorials and software examples included herewith are intended solely for
educational purposes. The material is provided in an “as is” condition. Any
express or implied warranties, including, but not limited to the implied warranties
of merchantability and fitness for particular purposes are disclaimed.

The software examples are self-contained low-level programs that typically


demonstrate a single peripheral function or device feature in a highly concise
manner. Therefore, the code may rely on the device's power-on default register
values and settings such as the clock configuration and care must be taken when
combining code from several examples to avoid potential side effects.
Additionally, the tutorials and software examples should not be considered for use
in life support devices or systems or mission critical devices or systems.

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.

Copyright © 2012-2015 Page 11 of 11


Valparaiso University

You might also like