• Aucun résultat trouvé

Using the break Keyword

Dans le document Click to visit (Page 146-150)

Here is some sample input and output:

Enter a positive number: 0

Number must be positive; please retry: -1 Number must be positive; please retry: 3 The number you entered is 3

This program would be more difficult to write with a for loop. While it could be done, the for loop is designed for situations in which the number of iterations is predictable.

Using the break Keyword

Even though the while loop is a better choice than a for loop for this program, which requires the user to enter a positive number, there are two problems with this program:

one minor and one major.

The minor problem is that there is some repetition of code; the user is requested both before and inside the loop to enter a positive number. A do while loop, which is explained in the following section, avoids this repetition, but repeats other code (there are tradeoffs in loops as well as in life).

The major problem is that the user is trapped inside the loop until they enter a positive number. That is not a good programming design. While the user should be required to enter good data if they are going to enter any data at all, they should have the option, when told the data entered was not valid, of quitting the data entry.

The following modification of the program uses the break keyword to provide the user with

file:///D|/((%20CMH%20Decompiled%20))/C++%20Demystified/8151final/LiB0053.html (3 of 9)06.11.2004 22:51:58

The While Loop

the option of quitting the data entry:

#include <iostream>

cout << "Enter a positive number: ";

cin >> num;

cout << "The number you entered is " << num << " ";

return 0;

}

Here is some sample input and output when the user eventually enters a positive number:

Enter a positive number: 0

Number must be positive; try again (Y/N): Y Enter number: -1

Number must be positive; try again (Y/N): Y Enter number: 3

The number you entered is 3

Here is some sample input and output when the user does not enter a positive number but instead decides to quit:

Enter a positive number: -2

Number must be positive; try again (Y/N): N The number you entered is -2

Flags

The flags modification is an improvement because the user no longer is trapped inside the loop until they enter a positive number, but instead has the option of quitting data entry. However, the second sample input and output, in which the user quits data entry, illustrates a problem. The final cout statement outputs the number entered, even if the number is invalid data.

Ideally, we would only want to output the data if it were valid. If the data were not valid, then we would want to output that fact instead. However, the code thus far does not enable us to differentiate whether the while loop ended because the user entered valid data or because the user decided to quit after entering invalid data.

In Chapter 7, I recommended that you use the break keyword sparingly because it created multiple exit points for the for loop, making your code more difficult to understand

file:///D|/((%20CMH%20Decompiled%20))/C++%20Demystified/8151final/LiB0053.html (4 of 9)06.11.2004 22:51:58

The While Loop

and increasing the possibility of logic errors. That advice also applies to the while loop. I recommended then, and recommend now, as an alternative the use of a logical operator.

The following program modification adopts that alternative.

#include <iostream>

cout << "Enter a positive number: ";

cin >> num;

Here is some sample input and output when the user eventually enters a positive number:

Enter a positive number: -3

Number must be positive; try again (Y/N): Y Enter number: 3

The number you entered is 3

Here is some sample input and output when the user does not enter a positive number but instead decides to quit. This time the final output is not of the number entered, but rather that the user did not enter a positive number:

Enter a positive number: 0

Number must be positive; try again (Y/N): Y Enter number: -1

Number must be positive; try again (Y/N): N You did not enter a positive number

This program modification, in addition to using the logical && operator, uses a Boolean variable named quit. This Boolean variable is used as a flag. A flag is a Boolean variable whose value indicates whether a condition exists.

In this program, the while loop continues to loop as long as the data entered is invalid and the user wants to keep going. Accordingly, the while keyword is followed by two

conditions, joined by the logical && operator.

file:///D|/((%20CMH%20Decompiled%20))/C++%20Demystified/8151final/LiB0053.html (5 of 9)06.11.2004 22:51:58

The While Loop

Note A common programming mistake in a while condition using a logical operator is to use && when you should use || or vice versa. While the logical && operator may seem the obvious choice in this example, the correct choice in other

situations may be less intuitive. For example, if you want to loop while a number is not between 1 and 10, would the loop be while (num < 1 && num > 10) or while (num < 0 || num > 10)? The answer is the latter; the condition always would be false using the && operator since a number cannot be both less than 1 and greater than 10. If you wanted to use the && operator, the condition instead would be while (num >= 1 && num <= 10).

The first condition is if num <= 0. If this expression is false, the data is valid, so the issue of whether the user wants to quit does not arise. Accordingly, the second condition, whether quit is true, is not even evaluated. As discussed in Chapter 7, with a logical &&

operator, the right expression is evaluated only if the left expression is true. Therefore, the while loop ends with the value of quit being false, its initialized value, and code execution continues with the if / else statement following the while loop.

However, if num <= 0 is true, then the data is invalid, and the second condition, whether quit is true, is evaluated.

The value of quit may be true under either of two possibilities. The first possibility is that this is the user’s first attempt to enter data and the data was invalid. In this case, the user has not yet been asked whether they want to quit. It is assumed they don’t, so they have the opportunity to answer whether they want to retry. Therefore, the quit variable is initialized to the value of false when it is declared.

The second possibility is that this is the user’s second or later attempt to enter data and the data was invalid. In this case, the user has already been asked whether they want to quit, so the value of quit is based on the user’s answer.

If the value of quit is false, the while loop continues. However, if the user wants to quit, then the right expression quit == false will be false because the value of quit is true.

Therefore, the while loop ends with the value of quit being true, and code execution continues with the if / else statement following the while loop.

At some point (hopefully) the while loop will end, either because the user has entered a valid number or has not and decided to quit trying. Code execution then continues with the if / else statement following the while loop.

The value of quit being false necessarily indicates that the user entered valid data, because if they were still trying to do so, the loop would not have ended. Conversely, the value of quit being true necessarily indicates that the user entered invalid data.

Accordingly, we use the value of quit in the if /else statement after the while loop to differentiate whether the while loop ended because the user entered valid data or instead decided to quit after entering invalid data.

Thus, inside the while loop, quit is a flag whose value indicates whether the user wants to try again, and after the while loop ends, quit is a flag whose value indicates whether the user entered valid data.

While (true)

In Chapter 7, we discussed the use of the for loop with the omission of the condition that is the second expression, such as for ( ; ; ). There, an infinite loop was avoided by using the break keyword inside the loop. While I did not recommend this use of the for loop, I

file:///D|/((%20CMH%20Decompiled%20))/C++%20Demystified/8151final/LiB0053.html (6 of 9)06.11.2004 22:51:58

The While Loop

mentioned it because you may encounter it as programmers do use the for loop this way.

Similarly, programmers sometimes make the condition of the while loop always true, such as while (true) or while (1), and break out of the while loop with, you guessed it, the break keyword. Here is an example that is a modification of the program we have been using that asks the user to enter a positive number.

#include <iostream>

The one advantage of this modification is that it renders unnecessary having to prompt the user both before and inside the loop to enter a positive number. However, the use of the while (true) syntax has the disadvantage of making your code less readable because the condition that stops the loop cannot be discerned from the parentheses following the while keyword. The do while loop (explained later in this chapter) avoids this

disadvantage and would be a preferable choice.

Dans le document Click to visit (Page 146-150)