Swift Comparison Operators

In Swift, comparison operators are used to compare two values. These operators return a Boolean value (true or false) based on whether the comparison is true or false. Comparison operators are commonly used in conditional statements and loops to make decisions in your program.


Comparison Operators in Swift

Here is a list of comparison operators available in Swift:

OperatorDescriptionExample
==Equal to5 == 5 returns true
!=Not equal to5 != 3 returns true
<Less than3 < 5 returns true
>Greater than5 > 3 returns true
<=Less than or equal to3 <= 5 returns true
>=Greater than or equal to5 >= 5 returns true
Comparison Operators in Swift

Example: Demonstrating All Comparison Operators

This example shows how each comparison operator works:

File: main.swift

</>
Copy
let a = 10
let b = 20

print("a == b: \(a == b)") // false
print("a != b: \(a != b)") // true
print("a < b: \(a < b)")   // true
print("a > b: \(a > b)")   // false
print("a <= b: \(a <= b)") // true
print("a >= b: \(a >= b)") // false

Explanation:

  • a == b: Checks if a is equal to b. Returns false because 10 is not equal to 20.
  • a != b: Checks if a is not equal to b. Returns true because 10 is not equal to 20.
  • a < b: Checks if a is less than b. Returns true because 10 is less than 20.
  • a > b: Checks if a is greater than b. Returns false because 10 is not greater than 20.
  • a <= b: Checks if a is less than or equal to b. Returns true because 10 is less than 20.
  • a >= b: Checks if a is greater than or equal to b. Returns false because 10 is not greater than 20.

Output:

Swift Example Output for  Demonstrating All Comparison Operators

Use Cases of Comparison Operators

Example 1: Using Comparison Operators in Conditional Statements

Comparison operators are often used in if statements to make decisions:

File: main.swift

</>
Copy
let temperature = 30

if temperature > 25 {
    print("It's a hot day!")
} else {
    print("The weather is pleasant.")
}

Explanation:

  • The temperature is compared to 25 using the > operator.
  • If the condition is true, the program prints “It’s a hot day!”; otherwise, it prints “The weather is pleasant.”

Output:

Swift Example Output for using comparison operators in conditional statements

Example 2: Using Comparison Operators in Loops

Comparison operators are frequently used in loops to control iterations:

File: main.swift

</>
Copy
for i in 1...5 {
    if i % 2 == 0 {
        print("\(i) is even")
    } else {
        print("\(i) is odd")
    }
}

Explanation:

  • The loop iterates through the numbers from 1 to 5.
  • The % operator checks if the number is divisible by 2.
  • The == operator is used to determine whether the remainder is equal to 0.

Output:

Swift Example Output for using Comparison Operators in loops

Conclusion

Comparison operators are essential for decision-making and controlling program flow in Swift. Understanding how to use these operators effectively will help you write more robust and dynamic code.