# 2.2 Comparison Operators

``````x == y    Equal to<br>
x != y    Not equal to<br>
x > y     Greater than<br>
x >= y    Greater than or equal to<br>
x < y     Less than<br>
x <= y    Less than or equal to<br>
x === y   Two objects are equal<br>
x !== y   Two objects are not equal
``````
• comparison operators return `true`/`false` and can be stored as a Bool
• must Type Cast to a common type (ie. all Ints, Doubles…)
``````let x = 100, y = 200<br>
<br>
// -> true<br>
let b1 = x < y
// -> false<br>
let b2 = x == y<br>
<br>
// -> true<br>
(1, "zebra") < (2, "apple")
// -> true<br>
(3, "apple") < (3, "bird")
// -> true<br>
(4, "dog") >= (4, "dog")
``````

## Ternary Conditional Operator

`Question ? <true value> : <false value>`

• returns one of two values based on if the question is true or false
``````let pop = 5000<br>
let pop2 = 2000<br>
<br>
// -> 2000<br>
let minimum = pop < pop2 ? pop : pop2
// -> small town<br>
let message = pop < 9999 ? "small town" : "\(pop) is big!"
// -> true<br>
pop >= 5000 ? "We're good" : "Oops, not quite"
``````

## Nil-Coalescing Operator `??`

• use when a variable might have a value or be nil
• if no value, then use a default value
``````var personalSite: String?
let defaultSite = "http://www.lyfebug.com"

// without ?? operator
let website: String
if personalSite != nil {
website = personalSite!
} else {
website = defaultSite
}

// with ?? operator
let website2 = personalSite ?? defaultSite
``````

## Identity Operators `===`

• used to determine if two vars/cons refer to the same instance of a class
• `===` → identical
• `!==` → not identical to
``````class C {<br>
var varX = 1<br>
}<br>
<br>
let classOne = C()<br>
let classTwo = C()<br>
let classThree = classOne<br>
<br>
// -> might be equal…<br>
if classOne === classTwo {<br>
print("Identical")<br>
} else {<br>
print("Maybe equal, but not identical")<br>
}<br>
<br>
// -> Yes<br>
if classOne === classThree {<br>
print("Yes, they refer to the same object.")<br>
}
``````