Swift Comparison Operators

Swift Comparison Operators

Use comparison operators to compare values: ==!=><>=<=.

They return Bool.


Compare Integers

Use ==!=><>=, and <= to compare numeric values.

The result is a Bool.

Example

let a = 5, b = 2
print(a == b)
print(a != b)
print(a > b)
print(a < b)
print(a >= 5)

This example prints the result of several comparisons.



Compare Strings

Strings compare lexicographically (dictionary order).

Comparison is case-sensitive.

Example

print("apple" < "banana")  // true
print("Swift" == "Swift")  // true
print("cat" > "car")       // true

This example compares strings using the same operators.

Comments

Leave a Reply

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