Table of contents
1.
Introduction
2.
If-else Statement
2.1.
Syntax of if Statement in Swift
2.1.1.
First Form
2.1.2.
Second Form
2.1.3.
Third Form
3.
Examples
3.1.
Example 1: if Statement
3.2.
Example 2: If-else Statement
3.3.
Example 3: Swift if-else if Statement
3.4.
Example 4: Nested if-else Statement
4.
Frequently Asked Questions
4.1.
What is the meaning of if-else? 
4.2.
What is a nested if-else example?
4.3.
Does else have to follow ‘if’?
5.
Conclusion
Last Updated: Mar 27, 2024

Swift If Else Statements

Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

In this blog, we will learn how to use the Swift if-else statement to create decision-making programmes using examples. 
When employing otherwise if, else sentences, there are a few things to bear in mind.
An if can have a value of zero or one, and it must come after any other ifs.
An if can have one or more else ifs, which must come before the else.
Once an else succeeds, none of the other else ifs or else are tested.
Let's move forward to knowing what if-else statements are and how to use them in Swift to create decision-making programs.

If-else Statement

When one or more meeting criteria are met, an if statement is used to run the code, and a piece of information can take one of two forms. The opening and closing braces are required in each form. 
The first form permits code execution only when a condition is met.
In computer programming, the if Statement is used to run a piece of code only when a specified condition is met.

For example, assigning grades (A, B, C) based on marks obtained by a student.
1. if the percentage is above 90, give a grade A
2. if the rate is above 75, give a grade of B
3. if the rate is above 65, give a grade of C

Syntax of if Statement in Swift

Swift's if statement syntax is as follows:

if (condition) {
  // statements
}

The if statement evaluates the condition contained within the parenthesis ().
If the condition is met, the code contained within the if the body is run.
If the condition is false, the code contained within the if statement is skipped.

First Form

The first form, which has the following syntax, allows code to be executed only when a condition is met: 

if condition {
    statements
}
 

Second Form

The second form of an if statement adds an else clause (initiated by the else keyword) and is used to execute one part of code when the condition is true and another part of code when the condition is false. When there is only one else clause, an if statement takes the following form:

if condition {
If the condition is true, statements will be executed. }
else {
If the condition is false, statements will be executed. }

Third Form

An if statement's else clause can contain another if statement to test more than one condition. An if statement chained in this manner takes the following form:

if condition 1 {
If the condition is true, statements will be executed. }
else if condition 2 {
If the condition is true, statements will be executed. }
else {
If the condition is false, statements will be executed. }

Examples

Example 1: if Statement

Code:

let number = 10

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

print("The statement is easy.")


Output:

Number is positive.
The statement is easy.

In the preceding example, we defined a variable called number. Take note of the test condition,

number > 0

Because the number is greater than zero, in this case, the condition is true.

If the variable is changed to a negative integer. Let's say it's -1.

let number = -1

When we run the programme now, the output will be as follows:

The statement is easy.

This is due to the fact that the value of a number is less than 0. As a result, the condition evaluates to false. The body of the if block is also skipped.

Example 2: If-else Statement

Code:

let number = 5

if (number > 0) {
    print("Positive Number.")
}
else {
    print("Negative Number.")
}
print("This statement is always executed.")

Output:

Positive Number.
This statement is always executed.

Example 3: Swift if-else if Statement

Code:

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

let number = 5

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

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

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

print("This statement is always executed")
 

Output:

Positive Number.

In the preceding example, we created a variable called a number with the value 0. We have two condition expressions here:

if (number > 0) - determines whether or not the number is greater than zero. if (number 0) - determines whether the number is less than zero.

In this case, both conditions evaluate as false. As a result, the statement within the body of else is executed.

Example 4: Nested if-else Statement

Code:

var number = 0
// outer if statement
if (number >= 0) {
  // inner if statement
  if (number == 0) {
      print("Number is 0")
  }
  // inner else statement
  else {
      print("Positive Number");
  }
}
// outer else statement
else {
    print("Negative Number");
}

Output:

Number is 0

In the preceding example, we used a nested if statement to determine whether the given number was positive, negative, or zero.


Check out this problem - First Missing Positive 

Frequently Asked Questions

What is the meaning of if-else? 

You can make a chain of if statements using the if/else if statement. The if statements are assessed one by one until one of them if expressions are true or the if/else if chain ends. There are no code blocks run if the if/else if chain reaches the end without a true statement.

 

What is a nested if-else example?

First, we check that out of the three numbers, the first two are equal. If they are, then we go inside the nested if to check whether the third is equal to them. If yes, then all are equal else they are not equal.

 

Does else have to follow ‘if’?

Only the if (expression) statement and the else statement are present. Because an if statement is a statement, you can use it after else just like any other statement, such as for or empty; or switch or any other arbitrary expression statement.

Conclusion

In this blog, we have extensively discussed the Swift If-else Statements. We hope that this article has provided you with additional information about the If-else statements in swift. And to learn in-depth about Android, check out the course on Android on the Coding Ninjas website. And also, check out the awesome content on the Coding Ninjas Website, Android DevelopmentCoding Ninjas Studio ProblemsCoding Ninjas Studio Interview BundleCoding Ninjas Studio Interview Experiences, and Coding Ninjas CoursesCoding Ninjas Studio Contests, and Coding Ninjas Studio Test SeriesDo upvote our blog in helping other ninjas grow.
Happy Coding!

Live masterclass