Logical Operators
Logical operators allow us to evaluate multiple Boolean values together in a single statement. We've seen previously that when we evaluate a condition, such as in an if
statement, we end up with a Boolean value. We can, therefore, use logical operators to combine and evaluate two or more conditions at one time.
We have three such operators available to us:
- AND (&&): This returns true when both conditions are true, and false otherwise.
- OR (||): This returns true when either condition is true, and false otherwise.
- NOT (!): This returns true if the condition is false, and true otherwise; essentially, it returns the opposite of the condition.
Let's take a look at how these operators work using an example.
Exercise 24: Logical Operators Example
To demonstrate how these logical operators work, let's create a quick example application. We'll take a number of inputs from the user, perhaps some names, and ...