• Aucun résultat trouvé

Using the switch Statement with Logical Operators

Dans le document Click to visit (Page 123-128)

Using the switch Statement with Logical Operators

The switch statement was discussed at some length in Chapter 5. However, so far in this chapter it has been conspicuous by its absence.

In Chapter 5, we discussed how the switch statement was cumbersome when dealing with a range of numbers. The reason was that the case keyword cannot be followed by a range of numbers because it must instead be followed by a single integer constant.

However, the switch statement may be used with expressions that use the logical And or Or operator. The reason is that these expressions have only one of two possible values, true or false. True and false are both constants; the value of true is always true and the value of false is always false. While true and false are Boolean values, each has a corresponding integer value: 1 and 0. Therefore, the case keyword may be followed by true or false, just as in Chapter 5 where the case keyword can be followed by a character since a character has a corresponding integer ANSI or ASCII value.

For example, earlier in this chapter the logical And operator was used in the following if/

else structure in determining whether the user is eligible to vote, the criteria being that the user must be at least 18 years old and a citizen.

if (age >= 18 && citizen == true)

Also earlier in this chapter, the logical Or operator was used in the following if/else

structure in determining whether the user gets into a movie free, the criteria being that the user must be either under 18 or at least 65 years old.

if (age <= 12 || age >= 65)

Using the switch Statement with Logical Operators

These examples illustrate that the switch statement can be employed as an alternative to an if / else or if / else if /else structure in programs that evaluate Boolean expressions using logical operators. However, it is not common for the switch statement to be employed in this manner because, with Boolean expressions, there are always just two alternatives, true and false, and switch statements generally are used when there are many more alternatives than two.

file:///D|/((%20CMH%20Decompiled%20))/C++%20Demystified/8151final/LiB0044.html (2 of 2)06.11.2004 22:51:50

Summary

Summary

In Chapter 5, we evaluated only one Boolean expression at a time to determine which of two alternative blocks of code should execute. However, often two (or more) Boolean expressions need to be evaluated to determine which block of code should execute. In the example in which you are eligible to vote only if the user is a citizen and at least 18 years old, both Boolean expressions must be true in order for the program to output that the user is eligible to vote. In another example, in which you get into a movie free if the user is either a senior citizen (65 years or older) or a child (12 or under), the program outputs that the user gets into the movie free if either Boolean expression is true.

This chapter covered two different approaches of evaluating two Boolean expressions to determine which code should execute. The first approach nested one if statement inside another. The second approach introduced three logical operators. The logical && (And) operator is used when both Boolean expressions must be true. The logical || (Or) operator is used when either Boolean expression must be true. Finally, the logical ! (Not) operator inverts the value of a Boolean expression, from true to false, or false to true.

Finally, this chapter showed how you can use the switch statement as an alternative to an if / else or if / else if /else structure in programs that evaluate Boolean expressions using logical operators.

file:///D|/((%20CMH%20Decompiled%20))/C++%20Demystified/8151final/LiB0045.html06.11.2004 22:51:50

Quiz

Quiz

1. Can you use nested if statements as an alternative to the logical And and Or operators?

2. Can an if statement be nested in the else if or else part of an if / else if / else statement, or just the if part?

3. For which of the logical operators do both Boolean expressions have to be true for the overall Boolean expression to be true?

4. For which of the logical operators do both Boolean expressions have to be false for the overall Boolean expression to be false?

5. Which of the logical operators reverses the “truth” of a Boolean expression, making a true expression false and a false expression true?

6. Assuming resident is a Boolean variable, is if(resident) the same as if(resident ==

true)?

7. Which of the logical operators is a unary rather than binary operator?

8. Which of the logical operators has a higher precedence than the relational operators?

9. Which logical operator has a higher precedence, And or Or?

10. Can a Boolean value of either true or false be used following the case keyword in a switch statement?

file:///D|/((%20CMH%20Decompiled%20))/C++%20Demystified/8151final/LiB0046.html06.11.2004 22:51:51

Chapter 7: The For Loop

Chapter 7: The For Loop

Overview

Parents customarily remind their children not to repeat themselves. Indeed, parents often illustrate another saying (“Do as I say, not as I do”) by continually repeating that reminder.

This is my nifty way of introducing the idea that, in the world of computers, sometimes you want your code to repeat itself, too. For example, if the user enters invalid data, you may want to ask the user whether they want to retry or quit. If they retry and still enter invalid data, you again would ask the user whether they want to retry or quit. This process keeps repeating until the user either enters valid data or quits.

You use a loop to repeat the execution of code statements. A loop in C++ is a structure that repeats the execution of code until a condition becomes false. In the preceding example, the condition is that the data is invalid and the user wants to retry, thus the repeating code is the prompt asking the user whether they want to retry or quit.

This chapter will show you how to use one type of loop: the for loop. However, before discussing the for loop, I’ll show you how to use increment and decrement operators, which are used in for and other types of loops. The next chapter will then show you how to use two other kinds of loops: the while loop and the do while loop.

file:///D|/((%20CMH%20Decompiled%20))/C++%20Demystified/8151final/LiB0047.html06.11.2004 22:51:52

Dans le document Click to visit (Page 123-128)