Demonstrated in Listing 6.1, a while loop causes your program to repeat a sequence of statements as long as the starting condition remains true.
counter: 1 counter: 2 counter: 3 counter: 4 counter: 5 Complete. counter: 5
The condition tested by a while loop can be as complex as any legal C++ expression. This can include expressions produced using the logical && (and), || (or), and ! (not) operators. Listing 6.2 is a somewhat more complicated while statement.
This program is a game. Enter two numbers, one small and one large. The smaller number will count up by ones; the larger number will count down by twos. The goal of the game is to guess when they'll meet. On lines 10–13, the numbers are entered. Line 18 sets up a while loop, which will continue only as long as three conditions are met: |
small is not bigger than large.
large isn't negative.
small doesn't overrun the size of a small integer (MAXSMALL).
On line 21 the value in small is calculated modulo 5,000. Since the modulo is the remainder, it only returns the value 0 when small is an exact multiple of 5,000. Remember that this does not change the value in small. Each time the value is 0, a dot (.) is printed to the screen to show progress. On line 24, small is incremented, and on line 26 large is decremented by 2.
When any of the three conditions in the while loop fails, the loop ends and execution of the program continues after the while loop's closing brace, on line 29.
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 might 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 6.3 demonstrates the use of these statements. This time the game has become more complicated. The user is invited to enter a small number, a large number, a skip number, and a target number. The small number will be incremented by 1, 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.
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
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 an unsigned short int, the body of the while loop is entered.
On line 30, 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 36, 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 45.
Both continue and break should be used with caution. Programs that suddenly branch from within the middle of loops are harder to understand, and liberal use of continue and break can render even a small while loop unreadable.
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. Because 1 is always true, the loop will never end, unless a break statement is reached. Listing 6.4 demonstrates counting to 10 using this construct.
counter: 11
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.
Infinite loops such as while(1) can cause your computer to hang up 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.
|
18.227.102.50