Conditionals in Swift ( If, Else If, Else )

Conditionals in Swift, Conditionals to make decisions in your Swift code, with if, else if, and else. If this happens, then do that. This is called control flow because you use it to control the way your code flows.

In this tutorial, you’ll learn how to use the if-statement in your Swift code. We’ll get into boolean logic, expressions, operators, and the syntax of if, else if, and else blocks.

if statement can be followed by an optional else if…else statement, which is very useful to test various conditions using a single if…else if statement.

In Swift, there are three forms of the if…else statement.

  • if statement
  • if…else statement
  • if…else if…else statement

When using if, else if, else statements, there are a few points to keep in mind.

  • An if can have zero or one else’s and it must come after any else if’s.
  • An if can have zero to many else if’s and they must come before the else.
  • Once an else if succeeds, none of the remaining else if’s or else’s will be tested.

Swift if Statement

if boolean_expression_1 {
   /* Code here */
} else if boolean_expression_2 {
   /* code here */
} else {
   /* Executes when the none of the above condition is true */
}

#Example

let number = 5

// check if number is greater than 0
if (number > 0) {
  print("Number is positive.")
}
print("The if statement is easy")

we have created a variable named number. Notice the test condition, number > 0
since the number is greater than 0, the condition evaluates true.

Swift if…else Statement

An if statement can have an optional else clause.

The syntax of the if-else statement is

if (condition) {
  // block of code if condition is true
}
else {
  // block of code if condition is false
}

The if…else statement evaluates the condition inside the parenthesis.

If the condition evaluates to true,

  • the code inside if is executed
  • the code inside else is skipped
#Example

let number = 5

if (number > 0) {
    print("Number is positive.")
}

else {
    print("Number is negative.")
}

print("This statement is always executed.")

Swift if…else if…else Statement

The if…else statement is used to execute a block of code among two alternatives.

However, if you need to make a choice between more than two alternatives, then we use the if…else if…else statement.

The syntax of the if…else if…else statement is

if (condition1) {
    // code block 1
}

else if (condition2){
    // code block 2
}

else {
    // code block 3
}

If condition1 evaluates to true, code block 1 is executed.
If condition1 evaluates to false, then condition2 is evaluated.
If condition2 is true, code block 2 is executed.
If condition2 is false, code block 3 is executed.

#Example 

let number = 0

if (number > 0) {
    print("Number is positive.")
}

else if (number < 0) {
    print("Number is negative")
}

else {
    print("Number is 0.")
}

print("This statement is always executed")

Swift nested if Statement

You can also use an if statement inside of an if statement. This is known as a nested if statement.

The syntax of nested if statement is

// outer if statement
if (condition1) {
    // statements

    // inner if statement
    if (condition2) {
    // statements
    }
}

Happy Swift Coding 🙂

Scroll to Top

Discover more from CODE t!ps

Subscribe now to keep reading and get access to the full archive.

Continue reading