continue and break statements in C++

By: Priya Emailed: 1728 times Printed: 2331 times    

Latest comments
By: rohit kumar - how this program is work
By: Kirti - Hi..thx for the hadoop in
By: Spijker - I have altered the code a
By: ali mohammed - why we use the java in ne
By: ali mohammed - why we use the java in ne
By: mizhelle - when I exported the data
By: raul - no output as well, i'm ge
By: Rajesh - thanx very much...
By: Suindu De - Suppose we are executing

At times you'll want to return to the top of a while loop before the entire set of statements in the while loop is executed. The continue statement jumps back to the top of the loop.

At other times, you may want to exit the loop before the exit conditions are met. The break statement immediately exits the while loop, and program execution resumes after the closing brace.

The following C++ program demonstrates the use of these statements. This time the game has become more complicated. The user is invited to enter a small number and a large number, a skip number, and a target number. The small number will be incremented by one, and the large number will be decremented by 2. The decrement will be skipped each time the small number is a multiple of the skip. The game ends if small becomes larger than large. If the large number reaches the target exactly, a statement is printed and the game stops.

The user's goal is to put in a target number for the large number that will stop the game.

break and continue.

1:    // \
2:    // Demonstrates break and continue
4:    #include <iostream.h>
6:    int main()
7:    {
8:      unsigned short small;
9:      unsigned long  large;
10:      unsigned long  skip;
11:      unsigned long target;
12:      const unsigned short MAXSMALL=65535;
14:      cout << "Enter a small number: ";
15:      cin >> small;
16:      cout << "Enter a large number: ";
17:      cin >> large;
18:      cout << "Enter a skip number: ";
19:      cin >> skip;
20:      cout << "Enter a target number: ";
21:      cin >> target;
23:    cout << "\n";
25:     // set up 3 stop conditions for the loop
26:      while (small < large && large > 0 && small < 65535)
28:      {
30:        small++;
32:         if (small % skip == 0)  // skip the decrement?
33:         {
34:           cout << "skipping on " << small << endl;
35:           continue;
36:         }
38:         if (large == target)    // exact match for the target?
39:         {
40:           cout << "Target reached!";
41:           break;
42:         }
44:         large-=2;
45:      }                   // end of while loop
47:      cout << "\nSmall: " << small << " Large: " << large << endl;
48:    return 0;
49: }
Output: Enter a small number: 2
Enter a large number: 20
Enter a skip number: 4
Enter a target number: 6
skipping on 4
skipping on 8

Small: 10 Large: 8

Analysis: In this play, the user lost; small became larger than large before the target number of 6 was reached.
On line 26, the while conditions are tested. If small continues to be smaller than large, large is larger than 0, and small hasn't overrun the maximum value for a small int, the body of the while loop is entered.

On line 32, the small value is taken modulo the skip value. If small is a multiple of skip, the continue statement is reached and program execution jumps to the top of the loop at line 26. This effectively skips over the test for the target and the decrement of large.

On line 38, target is tested against the value for large. If they are the same, the user has won. A message is printed and the break statement is reached. This causes an immediate break out of the while loop, and program execution resumes on line 46.


NOTE: Both continue and break should be used with caution. They are the next most dangerous commands after goto, for much the same reason. Programs that suddenly change direction are harder to understand, and liberal use of continue and break can render even a small while loop unreadable.

The continue Statement

continue; causes a while or for loop to begin again at the top of the loop. Example

if (value > 10)
     goto end;

if (value < 10)
     goto end;

cout << "value is 10!";


cout << "done";

The break Statement

break; causes the immediate end of a while or for loop. Execution jumps to the closing brace. Example

while (condition)
    if (condition2)
    // statements;

C++ Home | All C++ Tutorials | Latest C++ Tutorials

Sponsored Links

If this tutorial doesn't answer your question, or you have a specific question, just ask an expert here. Post your question to get a direct answer.

Bookmark and Share


1. View Comment

it's nice linking up here

View Tutorial          By: kasadha at 2010-10-28 10:34:36
2. View Comment

please tell me.........

View Tutorial          By: case c++ at 2012-02-20 02:45:55
3. View Comment

simple and exelent programs

View Tutorial          By: prasad at 2012-08-29 00:05:04

Your name (required):

Your email(required, will not be shown to the public):

Your sites URL (optional):

Your comments:

More Tutorials by Priya
Getting started with ASP
EJB Entity Beans
Error Handling - Stderr and Exit - sample program in C
Pointers and Arrays in C
Data Types and Sizes in C
virtual inheritance example in C++
ctime() sample program in C++
TextBox sample program in J2ME
Advantages of Java Beans
list() contents of a Directory - sample program in Java
File example program in Java
continue and break statements in C++
Use of 'throw' in Java
Passing parameters to a function by value in C++
Demonstrating global and local variables in C++

More Tutorials in C++
Two-Dimensional Array Manipulation in C++
Calculate average using Two-Dimensional Array in C++
Compute the square root of the sum of the squares of an array in C++
Matrix using nested for loops in C++
Sorting an array of Strings in C++
Calculating total based on the given quantity and price in C++
Compiling and Linking Multiple Source Files in C++
Enumerations in C++
Program to add two numbers in C++
Comments in C++
while loop in C++
for loop in C++
Programming errors a compiler will detect in C++
if in C++
Using the Built-in Arithmetic Types in C++

More Latest News
Most Viewed Articles (in C++ )
Dot (.) vs Arrow (->) to access data members in C++
Calculating total based on the given quantity and price in C++
Difference between Procedural, Structured, and Object-Oriented Programming
The indirection operator (*) - dereference operator.
strcat() and strncat() sample program in C++
Using peek() and putback() with cin in C++
Using cout.width() in C++
Sum of 1 to N in C++
Calculate average using Two-Dimensional Array in C++
Using cout in C++
Using Comments in a C++ Program
Use of Conditional (Ternary) Operator in C++
C++ Recursion function explained using Fibonacci series
Public versus Private members in C++
class keyword in C++
Most Emailed Articles (in C++)
Matrix using nested for loops in C++
The if Statement in C++
What Is a Pointer in C++?
C++ Destructors Versus Java Finalization
Operator Precedence in C++
How to handle Exceptions in C++
atoi(), itoa() in C++
Using cout.fill() in C++
Multiple inheritance example in C++
for loop in C++
Using the Built-in Arithmetic Types in C++
Two-Dimensional Array Manipulation in C++
Calculate average using Two-Dimensional Array in C++
Sorting an array of Strings in C++
A Brief History of C++