Subtitle

Booleans

Kicker

Aaron Cleveland
Dev Genius
Published in
3 min readMar 12, 2024

--

Do you like pizza? Does 5 + 5 equal 10? These seem like a couple of questions you have been asked before. Even though you might not use them all the time, they might come up depending on the type of application you’re building. But what about more of a technical question? Is this user authenticated? Is onboarding finished? These yes or no, and true and false questions can be very easy to use, implement, and save a lot of time checking the status and data of your application.

Booleans in Swift, represented by the Bool type, are essential for controlling the flow of a program using conditional statements and loops. They represent true or false values and play a crucial role in decision-making processes in code.

Conditional Statements with Swift Booleans

Swift provides several ways to use booleans in conditional statements:

  1. If Statement: Executes a block of code if the boolean condition is true.
let isRainy = true
if isRainy {
print("Don't forget your umbrella!")
}

2. If-Else Statement: Allows an alternative block of code to be executed if the condition is false.

let isSunny = false
if isSunny {
print("Wear sunscreen.")
} else {
print("You might need a jacket.")
}

3. Switch Statement: Although not common with booleans, switch statements can be used for more complex conditions.

let isWeekend = true
switch isWeekend {
case true:
print("It's time to relax!")
case false:
print("Another day at work.")
}

4. Ternary Conditional Operator: A shorthand for an if-else statement that is useful for assigning values based on a condition.

let isHungry = true
let action = isHungry ? "Eat something." : "Keep working."
print(action)

Practical Examples

Booleans are not just limited to control flow. They can be used in various scenarios:

  • Validating User Input:
let password = "123456"
let isValid = password.count >= 8
print(isValid ? "Password valid." : "Password is too short.")
  • Managing UI Elements:
var isButtonEnabled = false
// Based on some conditions, the button's state can be updated
isButtonEnabled.toggle() // Now isButtonEnabled is true
  • Loops: Using booleans to control the execution of loops.
var shouldContinue = true
var count = 0
while shouldContinue {
count += 1
print("Counting: \(count)")
if count == 5 {
shouldContinue = false
}
}

Boolean Logic

In Swift, boolean logic is used to evaluate multiple boolean expressions:

  • AND (&&): Both conditions must be true for the whole expression to be true.
  • OR (||): Only one of the conditions needs to be true for the whole expression to be true.
  • NOT (!): Inverts the value of a boolean, turning true to false and vice versa.
let hasInvitation = true
let knowsTheHost = false
if hasInvitation || knowsTheHost {
print("Welcome to the party!")
} else {
print("Sorry, you can't enter.")
}

Conclusion

By mastering the use of booleans in Swift, you can control the flow of your programs, make decisions based on conditions, and write cleaner and more maintainable code!

--

--