The logical operators are used with boolean values and expressions that
return boolean values. You use these operators to deal with expressions that
are made up of parts that can be either true or false. Logical operators are
used to test whether two expressions are true or whether one expression is
true. Table 18-1 describes the logical operators supported by Swift.
Table 18-1. Logical Operators
Operator Description
!x Logical NOT
x && y Logical AND
x || y Logical OR
For instance, let’s assume you already have two variables, x and y, with
values of true and false. To test and see whether both x and y are true, you
would use logical AND, as shown in Listing 18-1.
Listing 18-1. Logical AND
let x = true
let y = false
let a = x && y
In Listing 18-1, the value of a would be false because x and y are not
both true.
If you were interested only in whether one condition was true, you could use
logical OR to test for this, as shown in Listing 18-2.