Loops in C++: Keywords break and continue

The break and continue keywords provide additional controls in a loop.

You have used the keyword break in a switch statement. You can also use break in a loop to immediately terminate the loop. Listing 5.14 presents a program to demonstrate the effect of using break in a loop.

Listing 5.14 TestBreak.cpp

1 #include <iostream>

2 using namespace std;

3

4 int main()

5 {

6    int sum = 0;

7    int number = 0;

8

9     while (number < 20)

10    {

11        number++;

12        sum += number;

13        if (sum >= 100)

14        break;

15    }

16

17    cout << “The number is ” << number << endl;

18    cout << “The sum is ” << sum << endl;

19

20    return 0;

21 }

The program adds integers from 1 to 20 in this order to sum until sum is greater than or equal to 100. Without lines 13-14, this program would calculate the sum of the numbers from 1 to 20. But with lines 13-14, the loop terminates when sum becomes greater than or equal to 100. Without lines 13-14, the output would be

You can also use the continue keyword in a loop. When encountered, it ends the current iteration. Program control goes to the end of the loop body. In other words, continue breaks out of an iteration, while the break keyword breaks out of a loop. The program in Listing 5.15 shows the effect of using continue in a loop.

Listing 5.15  TestContinue.cpp

The program adds the integers from 1 to 20 except 10 and 11 to sum. The continue state­ment is executed when number becomes 10 or 11. The continue statement ends the current iteration so that the rest of the statement in the loop body is not executed; therefore, number is not added to sum when it is 10 or 11.

Without lines 12-13, the output would be as follows:

In this case, all the numbers are added to sum, even when number is 10 or 11. Therefore, the result is 210.

You can always write a program without using break or continue in a loop. See Check Point 5.28. In general, it is appropriate to use break and continue if their use simplifies coding and makes programs easy to read.

Suppose you need to write a program to find the smallest factor other than 1 for an integer n (assume n >= 2). You can write a simple and intuitive code using the break statement as follows:

int factor = 2;

while (factor <= n)

{

if (n % factor == 0)

break;

factor++;

}

cout << “The smallest factor other than 1 for “

<< n << ” is ” << factor << endl;

You may rewrite the code without using break as follows:

bool found = false;

int factor = 2;

while (factor <= n && found)

{

if (n % factor == 0)

found = true;

else

factor++;

}

cout << “The smallest factor other than 1 for “

<< n << ” is ” << factor << endl;

Obviously, the break statement makes this program simpler and easier to read. However, you should use break and continue with caution. Too many break and continue state­ments will produce a loop with many exit points and make the program difficult to read.

Source: Liang Y. Daniel (2013), Introduction to programming with C++, Pearson; 3rd edition.

Leave a Reply

Your email address will not be published. Required fields are marked *