Boolean Logical Operators

suggest change

The OR (||) operator returns true if either operand is true, and false otherwise. The following code prints because at least one side of the OR is true:

if (10 < 20) || (20 < 10) {
    print("Expression is true")
}

The AND (&&) operator returns true only if both operands are true. The following example does not print, because only one operand is true:

if (10 < 20) && (20 < 10) {
    print("Expression is true")
}

Swift has no boolean XOR operator (^ is bitwise, for integers). For two Bool values, != is exclusive-or: true when exactly one operand is true.

if (10 < 20) != (20 < 10) {
    print("Expression is true")
}

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents