Content Index
- What are conditionals in Swift and what are they used for
- The if structure in Swift (with simple examples)
- Comparison operators and logical operators in Swift
- AND (&&), OR (||) and NOT (!)
- Conditional flows
- More complete conditions: nested if and switch
- Multiple cases, ranges, and mandatory default
- Ternary operator in Swift: what it is and when to use it
- Common errors when using conditionals in Swift
- Conclusion and next steps in Swift
When you start developing iOS applications with Swift, one of the first concepts you need to master are conditionals. Thanks to them, your application can make decisions and execute different blocks of code depending on whether a specific condition is met or not.
In Swift, conditional structures are clear, strict, and highly oriented toward preventing errors—something that is greatly appreciated when you are learning or building more complex logic in a real app.
We stayed on the topic of using functions in Swift.
What are conditionals in Swift and what are they used for
A conditional is a control structure that allows code to be executed only if a condition is met. That condition always evaluates to a boolean value (true or false).
For me, without exaggeration and being a bit sarcastic, conditionals are THE BEST part of programming, by allowing the execution of this or that based on a condition.
In the day-to-day of iOS development, conditionals are used constantly, for example to:
- Validate data entered by the user.
- Show or hide views.
- Check statuses (login, permissions, errors).
- Make decisions based on numerical values or text.
The if structure in Swift (with simple examples)
The most basic conditional structure in Swift is if. Its syntax is very clean:
var myVar = true
if myVar {
print("Hello World")
}If the condition (myVar) is true, the code inside the block is executed. If it is false, it is simply ignored.
When I started with Swift, understanding that a condition can be read literally as “if this is true, then execute this code” helped me a lot to stop being afraid of logic.
Mandatory use of braces and best practices
- A best practice (and in Swift, a requirement) is to use braces {} to enclose the code that will run when the condition is met. Unlike other languages, Swift does not allow omitting them, which reduces errors and improves readability.
This detail seems small, but when you start writing more complex conditionals, always having well-defined blocks makes the code much easier to understand and maintain.
var myVar = true
if myVar {
print("Hello World")
}As mentioned before, braces are mandatory in Swift and in any type of structure we are going to use.
if else in Swift: what happens when the condition is not met
Many times it is not enough to execute code only when the condition is true. We also need to define what happens when it is not met. For that, we use else.
Since you can read the condition as "if myVar is true then..." and that is where you execute the code defined by the conditional, in the previous example you see the message on the screen because the condition is true: to evaluate the conditions you can use any of the comparison methods we saw previously.
You can also use if..else to execute code when the condition is false: specifically, the else is used:
var myVar = true
if myVar {
print("myVar is true")
}
else {
print("myVar is false")
}Here the flow is clear:
- If myVar is true, the first block is executed.
- If it is false, the else block is executed.
This pattern is very common in validations and messages to the user.
Comparison operators and logical operators in Swift
To build conditions, Swift offers comparison operators:
- > greater than
- < less than
- >= greater than or equal to
- <= less than or equal to
- == equal to
- != different from
Simple example:
if number > 22 {
print("number is greater than 22")
}AND (&&), OR (||) and NOT (!)
You can also combine conditions using logical operators:
- && (AND): both conditions must be true.
- || (OR): at least one must be true.
- ! (NOT): negates the boolean value.
if assistance && (exempt || grade >= 8)
{
print("You passed the exam")
}
Understanding the precedence of these operators well and using parentheses when necessary avoids many hard-to-detect errors.
Conditional flows
Let's see some examples of how conditionals work; for example, we want to execute the following print when the variable number is greater than 22:
var number = 24
if number > 22 {
print("number is greater than 22")
}In this case, when executing the code, it will show the text that says:
number is greater than 22But if we change the value of the variable number from 24 to 14 and execute:
var number = 14
if number > 22 {
print("number is greater than 22")
}Nothing will appear: and we must notify our user that the value is greater than 25; for that, we can use else:
And this time the code does execute and prints that "number is less than or equal to 22".
var number = 14
if number > 22 {
print("number is greater than 22")
} else {
print("number is less than or equal to 22")
}More complete conditions: nested if and switch
If statements can be nested when you need to evaluate conditions within other conditions. Although valid, it should be used carefully so as not to overly complicate the program flow.
If you notice that you are nesting many if statements, it is usually a sign that you could simplify the logic or use a switch.
Now, what if we want to evaluate more than one condition, or rather, we want to evaluate multiple conditions? Suppose we want to evaluate if the number is equal to 10, 20, 30, and so on; we could use the following conditional:
var number = 10
if number == 10 {
print("number is equal to 10")
} else if number == 20 {
print("number is equal to 20")
} else if number == 30 {
print("number is equal to 30")
}Although this, as you can see, can get a bit messy, we can use another control structure that works better for these cases known as switch:
var number = 10
switch number {
case 10:
print("number is 10")
case 20:
print("number is 20")
case 30:
print("number is 30")
case 40:
print("number is 40")
case 50:
print("number is 50")
default:
print("number is something else")And we must provide a default case obligatorily, so that the switch structure must always be exhaustive.
Multiple cases, ranges, and mandatory default
In Swift, the default is mandatory, which guarantees that the switch always has an execution path.
Additionally, you can use ranges:
let grade: Double = 4.5
switch grade {
case 0...2:
print("Failed")
case 2...2.9:
print("It was close")
case 3...3.9:
print("Passed")
case 4...5:
print("Excellent")
default:
print("Invalid grade")This type of logic is much clearer than several chained else if statements.
Ternary operator in Swift: what it is and when to use it
The ternary operator is a short way to write a simple if else:
let driverLicense = true
driverLicense ? print("Driver's Seat") : print("Passenger's Seat")It is useful for quick assignments, but in my experience, it is best to use it only when the condition is very clear. Otherwise, readability suffers.
Common errors when using conditionals in Swift
Some typical errors when you are starting out:
- Using = instead of == to compare.
- Creating overly complex conditions without parentheses.
- Abusing nested ifs.
- Not choosing switch when it is the best option.
Avoiding these mistakes from the beginning makes your code cleaner and more professional.
Conclusion and next steps in Swift
Conditionals in Swift are the foundation for creating smart and dynamic iOS applications. Mastering if, else, else if, switch, and logical operators will allow you to write clearer, safer, and easier-to-maintain code.
From here, the natural next step is to combine these structures with functions, arrays, and optionals, where conditionals become even more important.
Next step, learn how to use loops in Swift: for, in, and while