C++

CONTINUE AND BREAK

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.

Listing 7.4 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.

Listing 7.4. break and continue.

1: // Listing 7.4

2: // Demonstrates break and continue

3:

4: #include <iostream.h>

5:

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;

13:

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;

22:

23: cout << "\n";

24:

25: // set up 3 stop conditions for the loop

26: while (small < large && large > 0 && small < 65535)

27:

28: {

29:

30: small++;

31:

32: if (small % skip == 0) // skip the decrement?

33: {

34: cout << "skipping on " << small << endl;

35: continue;

36: }

37:

38: if (large == target) // exact match for the target?

39: {

40: cout << "Target reached!";

41: break;

42: }

43:

44: large-=2;

45: } // end of while loop

46:

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!";

end:

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)

break;

// statements;

}

while (1) Loops

The condition tested in a while loop can be any valid C++ expression. As long as that condition remains true, the while loop will continue. You can create a loop that will never end by using the number 1 for the condition to be tested. Since 1 is always true, the loop will never end, unless a break statement is reached. Listing 7.5 demonstrates counting to 10 using this construct.

Listing 7.5. while (1) loops.

1: // Listing 7.5

2: // Demonstrates a while true loop

3:

4: #include <iostream.h>

5:

6: int main()

7: {

8: int counter = 0;

9:

10: while (1)

11: {

12: counter ++;

13: if (counter > 10)

14: break;

15: }

16: cout << "Counter: " << counter << "\n";

17: return 0;

18:

Output: 

Counter: 11

Analysis: On line 10, a while loop is set up with a condition that can never be false. The loop increments the counter variable on line 12 and then on line 13 tests to see whether counter has gone past 10. If it hasn’t, the while loop iterates. If counter is greater than 10, the break on line 14 ends the while loop, and program execution falls through to line 16, where the results are printed. This program works, but it isn’t pretty. This is a good example of using the wrong tool for the job. The same thing can be accomplished by putting the test of counter’s value where it belongs—in the while condition.

WARNING: Eternal loops such as while (1) can cause your computer to hang if the exit condition is never reached. Use these with caution and test them thoroughly.

C++ gives you many different ways to accomplish the same task. The real trick is picking the right tool for the particular job.

DON’T use the goto statement. DO use while loops to iterate while a condition is true. DO exercise caution when using continue and break statements. DO make sure your loop will eventually end.

do...while Loops

It is possible that the body of a while loop will never execute. The while statement checks its condition before executing any of its statements, and if the condition evaluates false, the entire body of the while loop is skipped. Listing 7.6 illustrates this.

Listing 7.6. Skipping the body of the while Loop.

1: // Listing 7.6

2: // Demonstrates skipping the body of

3: // the while loop when the condition is false.

4:

5: #include <iostream.h>

6:

7: int main()

8: {

9: int counter;

10: cout << "How many hellos?: ";

11: cin >> counter;

12: while (counter > 0)

13: {

14: cout << "Hello!\n";

15: counter—;

16: }

17: cout << "Counter is OutPut: " << counter;

18: return 0;

19: }

Output: 

How many hellos?: 2

Hello!

Hello!

Counter is OutPut: 0

How many hellos?: 0

Counter is OutPut: 0

Analysis: The user is prompted for a starting value on line 10. This starting value is stored in the integer variable counter. The value of counter is tested on line 12, and decremented in the body of the while loop. The first time through counter was set to 2, and so the body of the while loop ran twice. The second time through, however, the user typed in 0. The value of counter was tested on line 12 and the condition was false; counter was not greater than 0. The entire body of the while loop was skipped, and Hello was never printed. What if you want to ensure that Hello is always printed at least once? The while loop can’t accomplish this, because the if condition is tested before any printing is done. You can force the issue with an if statement just before entering the while:

if (counter < 1) // force a minimum value

counter = 1;

but that is what programmers call a "kludge," an ugly and inelegant solution.

do...while

The do...while loop executes the body of the loop before its condition is tested and ensures that the body always executes at least one time. Listing 7.7 rewrites Listing 7.6, this time using a do...while loop.

Listing 7.7. Demonstrates do...while loop.

1: // Listing 7.7

2: // Demonstrates do while

3:

4: #include <iostream.h>

5:

6: int main()

7: {

8: int counter;

9: cout << "How many hellos? ";

10: cin >> counter;

11: do

12: {

13: cout << "Hello\n";

14: counter—;

15: } while (counter >0 );

16: cout << "Counter is: " << counter << endl;

17: return 0;

18: }

Output:

How many hellos? 2

Hello

Hello

Counter is: 0

Analysis: The user is prompted for a starting value on line 9, which is stored in the integer variable counter. In the do...while loop, the body of the loop is entered before the condition is tested, and therefore the body of the loop is guaranteed to run at least once. On line 13 the message is printed, on line 14 the counter is decremented, and on line 15 the condition is tested. If the condition evaluates TRUE, execution jumps to the top of the loop on line 13; otherwise, it falls through to line 16. The continue and break statements work in the do...while loop exactly as they do in the while loop. The only difference between a while loop and a do...while loop is when the condition is tested.

Back to Index