Swift if, if...else Statement

In computer programming, we use the if statement to run a block code only when a certain condition is met.

For example, assigning grades (A, B, C) based on marks obtained by a student.

  1. if the percentage is above 90, assign grade A
  2. if the percentage is above 75, assign grade B
  3. if the percentage is above 65, assign grade C

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

  1. if statement
  2. if...else statement
  3. if...else if...else statement

1. Swift if Statement

The syntax of if statement in Swift is:

if (condition) {
  // body of if statement
}

The if statement evaluates condition inside the parenthesis ().

  • If condition is evaluated to true, the code inside the body of if is executed.
  • If condition is evaluated to false, the code inside the body of if is skipped.

Note: The code inside { } is the body of the if statement.

Code inside the body of if is executed if the condition is true.
Working of if statement

Example 1: if Statement

let number = 10

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

print("The if statement is easy")

Output

Number is positive.
The if statement is easy

In the above example, we have created a variable named number. Notice the test condition,

number > 0

Here, since number is greater than 0, the condition evaluates true.

If we change the variable to a negative integer. Let's say -5.

let number = -5

Now, when we run the program, the output will be:

The if statement is easy

This is because the value of number is less than 0. Hence, the condition evaluates to false. And, the body of if block is skipped.


2. Swift if...else Statement

An if statement can have an optional else clause.

The syntax of 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,

  1. the code inside if is executed
  2. the code inside else is skipped

If the condition evaluates to false,

  1. the code inside else is executed
  2. the code inside if is skipped
How if...else statement works
Working of if...else statement

Example 2: Swift if...else Statement

let number = 10

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

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

print("This statement is always executed.")

Output

Number is positive.
This statement is always executed.

In the above example, we have created a variable named number. Here, the test expression

number > 0

Since the value of number is 10, the test expression evaluates to true. Hence code inside the body of if is executed.

If we change the variable to a negative integer. Let's say -5.

let number = -5

Now if we run the program, the output will be:

Number is negative.
This statement is always executed.

Here, the test expression evaluates to false. Hence code inside the body of else is executed.


3. 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
}

Here,

  1. If condition1 evaluates to true, code block 1 is executed.
  2. If condition1 evaluates to false, then condition2 is evaluated.
    1. If condition2 is true, code block 2 is executed.
    2. If condition2 is false, code block 3 is executed.
How if...else if...else statement works
Working of if...else if statement

Example 3: Swift if..else if Statement

// check whether a number is positive, negative, or 0.

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")

Output

Number is 0.

In the above example, we have created a variable named number with the value 0. Here, we have two condition expressions:

  1. if (number > 0) - checks if number is greater than 0
  2. else if (number < 0) - checks if number is less than 0

Here, both the conditions evaluate to false. Hence the statement inside the body of else is 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
    }
}

Notes:

  1. We can add else and else if statements to the inner if statement as required.
  2. We can also insert inner if statement inside the outer else or else if statements(if they exist)
  3. We can nest multiple layers of if statements.

Example 4: Nested if...else Statement

var number = 5

// outer if statement
if (number >= 0) {

  // inner if statement
  if (number == 0) {
      print("Number is 0")
  }

  // inner else statement
  else {
      print("Number is positive");
  }
}

// outer else statement
else {
    print("Number is negative");
}

Output

Number is positive

In the above example, we have used a nested if statement to check whether the given number is positive, negative, or 0.

Did you find this article helpful?