Use Comparison Operators in Swift
Written by Team Kodeco
Written by Team Kodeco
Swift provides a set of basic comparison operators that can be used to compare values and determine their relative order. The basic comparison operators in Swift are:
-
==
for equality. -
!=
for inequality. -
>
for greater than. -
<
for less than. -
>=
for greater than or equal to. -
<=
for less than or equal to.
Here’s an example of using comparison operators to compare values:
let a = 5
let b = 2
let isEqual = a == b // false
let isNotEqual = a != b // true
let isGreaterThan = a > b // true
let isLessThan = a < b // false
let isGreaterThanOrEqual = a >= b // true
let isLessThanOrEqual = a <= b // false
print("isEqual: \(isEqual)")
print("isNotEqual: \(isNotEqual)")
print("isGreaterThan: \(isGreaterThan)")
print("isLessThan: \(isLessThan)")
print("isGreaterThanOrEqual: \(isGreaterThanOrEqual)")
print("isLessThanOrEqual: \(isLessThanOrEqual)")
// Output: isEqual: false
// isNotEqual: true
// isGreaterThan: true
// isLessThan: false
// isGreaterThanOrEqual: true
// isLessThanOrEqual: false
Prev chapter
1.
Use Arithmetic Operators in Swift
Next chapter
3.
Use Logical Operators in Swift