Boolean Logical Operators
suggest changeThe 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")
}
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents