In this tutorial, you will learn about the Swift if…else statement with the assistance of examples to create decision-making programs.
The Swift if-else statement contains two statements: if statement and else statement.
On the off chance that the test evaluation is true, the if statement is executed and if the test evaluation is false, then the else statement is executed.
In computer programming, we use the if statement to run a block code just when a specific condition is met.
For instance, assigning grades (A, B, C) based on marks obtained by a student.
- if the percentage is above 90, assign grade A
- if the percentage is above 75, assign grade B
- if the percentage is above 65, assign grade C
In Swift, there are three types of the if…else statement.
- if statement
- if…else statement
- if…else if…else statement
In this article, you will learn-
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 enclosure ().
- In the event that condition is evaluated to true, the code inside the body of if is executed.
- In the event that 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.
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.
In the event that we change the variable to a negative number. Suppose – 5.
let number = -5
Presently, when we run the program, the output will be:
The if statement is easy
This is on the grounds that the worth of the number is under 0. Subsequently, the condition evaluates to false. What’s more, the body of if block is skipped.
2. Swift if…else Statement
An if statement can have an optional else clouse.
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 bracket.
In the event that the condition evaluates to true,
- the code inside if is executed
- the code inside else is skipped
In the event that the condition evaluates to false,
- the code inside else is executed
- the code inside if is skipped
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 worth of the number is 10, the test expression evaluates to true. Thus code inside the body of if is executed.
In the event that we change the variable to a negative integer. Suppose – 5.
let number = -5
Presently in the event that we run the program, the output will be:
Number is negative. This statement is always executed.
Here, the test expression evaluates to false. Consequently 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 other options.
In any case, in the event that you need to settle on a decision between multiple other options, 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,
- 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 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 worth 0. Here, we have two condition expressions:
- if (number > 0) – checks if number is greater than 0
- else if (number < 0) – checks if number is less than 0
Here, both the conditions evaluates to false. Thus the statement inside the body of else is executed.
Swift nested if Statement
You can likewise use an if statement within 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:
- We can add else and else if statements to the inner if statement as required.
- We can likewise insert inner if statement inside the external else or else if statements(if they exist)
- We can settle numerous 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.
Thanks for reading! We hope you found this tutorial helpful and we would love to hear your feedback in the Comments section below. And show us what you’ve learned by sharing your photos and creative projects with us.