Chapter    18

Logical Operators

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 = truelet y = ...

Get Swift Quick Syntax Reference now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.