Swift if Statement

In programming, you often use the if statement when you want to execute different code depending on certain conditions.

In this article, we'll go over the basics of the if statement in Swift.

Basics of the if statement in Swift

The if statement executes the following code block when the condition evaluates to true.

The syntax of an if statement in Swift is as follows:

if [condition A] {
    [code block A]
} else if [condition B] {
    [code block B]
} else if [condition C] {
    [code block C]
} else {
    [code block D]
}

Here, else if and else are optional, and you can chain as many else if clauses as needed.

First, the if statement evaluates [condition A]. If it's true, [code block A] will run.

If the condition is false, Swift checks the else if conditions in order, executing the first one that evaluates to true.

Once a matching condition is found and its code block is executed, the rest of the conditions are ignored.

If none of the conditions are true, the else block will run.


Let's try using an if statement.

What will the result be for the following code?

var a = 4
var msg: String

if a == 5 {
    msg = "a is 5."
} else if a != 4 {
    msg = "a is not 4."
} else if a > 2 {
    msg = "a is greater than 2."
} else {
    msg = "None of the above conditions matched."
}
print(msg)

Swift evaluates the conditions from top to bottom: a == 5 is false, a != 4 is also false, but a > 2 is true. Therefore, the code msg = "a is greater than 2." is executed.

When you print(msg), you'll see “a is greater than 2.” in the output.

Swift if Statement 1

if Statements with Multiple Conditions

You can combine multiple conditions using the && (and) or || (or) operators.

For example, if you want to execute some code when a is not 4 and is greater than 6, you can use && like this:

var a = 7
var msg: String

if a != 4 && a > 6 {
    msg = "a is not 4 and is greater than 6."
} else {
    msg = "a is 4 or less than or equal to 6."
}
print(msg)

Since 7 is not 4 and is greater than 6, the if condition evaluates to true. The output will be as follows:

Swift if Statement 2


Now, let's change a to 5 and run the code again.

Swift if Statement 3

This time, a != 4 is true, but a > 6 is false. Since && requires both conditions to be true, the if block is skipped, and the else block runs instead.


Ternary Conditional Operator in Swift

The ternary conditional operator is a shorthand way to write an if-else statement more concisely.

In Swift, the ternary operator uses ? : and looks like this:

[condition] ? [value if true] : [value if false]

For example, take the following code:

var a = 4
var msg: String

if a == 5 {
    msg = "a is 5."
} else {
    msg = "a is not 5."
}
print(msg)

This can be rewritten using the ternary conditional operator as follows:

var a = 4
var msg: String

msg = a == 5 ? "a is 5." : "a is not 5."
print(msg)

When you run it, a == 5 is false, so msg gets the value “a is not 5.”

Swift if Statement 4


That wraps up our explanation of the if statement in Swift.