3.2. Logical Operators
The tests you have put in the if expressions have been relatively simple so far. Real life is typically more complicated. You will often want to combine a number of conditions so that you execute a particular course—for example, if they are all true simultaneously. You can ride the roller coaster if you are over 12 years old, over 4 feet tall, and less than 6 feet 6. Failure on any count and it's no-go. Sometimes, though, you may need to test for any one of a number of conditions being true—for example, you get a lower price entry ticket if you are under 16, or over 65.
You can deal with both of these cases, and more, using logical operators to combine several expressions that have a value true or false. Because they operate on boolean values, they are also referred to as boolean operators. There are five logical operators that operate on boolean values:
|!||logical negation (NOT)|
These are very simple; the only point of potential confusion is the fact that you have the choice of two operators for each of AND and OR. The extra operators are the bitwise & and | from the previous chapter that you can also apply to boolean values where they have an effect that is subtly different from && and ||. I'll first consider what each of these is used for in general terms; then I'll look at how you can use them in an example.
3.2.1. Logical AND Operations
You can use either AND operator, && or &, where ...