Swift Logical Operators

Swift Logical Operators

Combine booleans with && (AND), || (OR), and ! (NOT).


Logical AND (&&)

Both must be true for the result to be true.

Example

let isOwner = true
let isAdmin = false
print(isOwner && isAdmin)
print(true && true)

Logical OR (||)

At least one true makes the result true.

Example

let a = true
let b = false
print(a || b)
print(false || false)


Logical NOT (!)

Flip a boolean value: true becomes false and vice versa.

Example

let flag = false
print(!flag)
print(!true)

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *