Beyond numbers and text, programs often need to represent simple "yes" or "no" states, like whether a light switch is on or off.
Swift has a specific data type for this kind of information.
Engagement Message
Can you think of a situation where you only need a true/false answer?
This data type is called a Boolean, named after George Boole. In Swift code, it's shortened to Bool
.
A Boolean variable can only hold one of two possible values: true
or false
.
Engagement Message
Why do you think these specific words are used?
It's crucial to remember that true
and false
must be lowercase in Swift. True
or False
(capitalized) won't work.
Engagement Message
How do you think is different from storing the word "true" as a string?
Booleans are fundamental for decision-making in code. We often get Boolean values by comparing other values.
For example, we can check if two numbers are equal using ==
(double equals sign).
