Swift if and if…else Statement


In Swift, the if statement and if…else statement are used to test various conditions in the program to perform different tasks depending upon the condition.

If you are already familiar with any other programming language, then you will find that the role of if and if…else statements in Swift is the same as other programming languages that are testing conditions.

using if…else statement we can perform decision-making tasks in our program.

For example, we can determine the fast, normal and slow speed of a car based on the speed.

If the speed of the car is above 60km/hour, it will be fast.

If the speed of the car is below 60km/hour, it will be normal.

If the speed of a car is below 30km/hour, it will be slow.

Swift provides if…else statement in three formats. These are if statement, if...else statement and the if…else if…else statement.

if statement in Swift

The single if statement in Swift is used to check only one condition inside the parenthesis. If the condition is evaluated to be true, then the code inside the curly brackets will be executed. Otherwise, the code will be skipped.

Below is the syntax for Swift if statement:

if (condition) {
  // code block of the body
}
// Code block after if statement

Now below is the example:

var num = 5

if(num < 10) {
    print("The number is less than 10")
}

print("Playing with if statement")

Output:

The number is less than 10
Playing with if statement

In the above example, we are checking the number num if it is less than 10 or not.

Now see another example:


var num = 5

if(num == 5) {
    print("The number is equal to 5")
}
print("Playing with if statement")

In the above example, we are checking the condition if the number num is equal to 5 or not.

Below is the diagram of the Swift if statement:

swift if statement diagram

if…else statement in Swift

We can use the optional else clause with the if statement. Below is the syntax:

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

If the condition is true then it will run the code inside the body of if statement, otherwise it will run the code in the else clause. The else clause will run the code block if the condition is false.

Below is an example:

var num = 5

if(num == 5) {
    print("The number is equal to 5")
} else {
    print("The number is not equal to 5")
}

In the above code id the number num is equal to 5, then the condition will be true and the code block inside if will be run.

If the number num is not equal to 5 then the code inside if will skip and the code inside else will run.

Below is the diagram of Swift if...else statement for both true and false conditions:

swift if else statement diagram

if…else if…else statement

If you need to check conditions for more than two alternatives, then the if…else if…else statement will be useful. It can check conditions for more than two alternatives.

Below is the syntax for if…else if…else statement :

if (condition1) {
    // Block of code 1
}

else if (condition2){
    // Block of code 2
}

else {
    // Block of code 3
}

Now see the example below:

var mark = 75

if(mark > 80) {
    print("Grade is A")
} else if(mark > 60) {
    print("Grade is B")
} else {
    print("Grade is C")
}

Output:

Grade is B

In the above code, we are checking the marks of students and evaluating their grades. Grade A for a mark over 80, grade B for a mark over 60, and if both of these will not satisfy, the grade will be C.

Below is the diagram of if…else if…else statement:

if…else if…else statement