Comparison operators

You’ve seen a few types now, such as Int, Double and String. Here you’ll learn about another type, one that will let you compare values through the comparison operators.

When you perform a comparison, such as looking for the greater of two numbers, the answer is either true or false. Swift has a data type just for this! It’s called a Bool, which is short for Boolean, after a rather clever man named George Boole who invented an entire field of mathematics around the concept of true and false.

This is how you use a Boolean in Swift:

let yes: Bool = true let no: Bool = false

And because of Swift’s type inference, you can leave off the type:

let yes = true let no = false

A Boolean can only be either true or false, denoted by the keywords true and false. In the code above, you use the keywords to set the initial state of each constant.

results matching ""

    No results matching ""