Mastering Swift Conditional Statements: A Comprehensive Guide Learn how to write robust and efficient code with Swift’s powerful `if` statement, including the simple `if` statement, `if…else` statement, and `if…else if…else` statement, as well as nested `if` statements.

Unlock the Power of Conditional Statements in Swift

When it comes to writing robust and efficient code, conditional statements are a crucial element in any programming language. In Swift, the if statement is a fundamental building block that allows you to execute a block of code only when a certain condition is met.

The Basics of the if Statement

Imagine assigning grades to students based on their marks. You can use the if statement to evaluate the percentage and assign a grade accordingly. For instance:

if percentage > 90 {
grade = "A"
} else if percentage > 75 {
grade = "B"
} else if percentage > 65 {
grade = "C"
}

Exploring the Three Forms of the if…else Statement

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

The Swift if Statement

The syntax of the if statement is straightforward:

if (condition) {
// code to execute if condition is true
}

If the condition evaluates to true, the code inside the body of the if statement is executed. Otherwise, it’s skipped.

Example 1: if Statement Output

Let’s create a variable number and test whether it’s greater than 0:

let number = 5
if number > 0 {
print("Number is positive")
}

If we change the value of number to a negative integer, say -5, the output will be:

// no output

This is because the condition evaluates to false, and the body of the if statement is skipped.

The Swift if…else Statement

An if statement can have an optional else clause. The syntax is:

if (condition) {
// code to execute if condition is true
} else {
// code to execute if condition is false
}

If the condition evaluates to true, the code inside the if block is executed, and the else block is skipped. If the condition evaluates to false, the else block is executed, and the if block is skipped.

Example 2: Swift if…else Statement Output

Let’s create a variable number and test whether it’s greater than 0:

let number = 10
if number > 0 {
print("Number is positive")
} else {
print("Number is negative or zero")
}

If we change the value of number to a negative integer, say -5, the output will be:

Number is negative or zero

The Swift if…else if…else Statement

When you need to make a choice between more than two alternatives, you can use the if...else if...else statement. The syntax 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, condition2 is evaluated. If condition2 is true, code block 2 is executed. If both condition1 and condition2 evaluate to false, code block 3 is executed.

Example 3: Swift if…else if…else Statement Output

Let’s create a variable number and test whether it’s greater than 0, less than 0, or equal to 0:

let number = 0
if number > 0 {
print("Number is positive")
} else if number < 0 {
print("Number is negative")
} else {
print("Number is zero")
}

In this case, both conditions evaluate to false, so the statement inside the else block is executed:

Number is zero

Swift Nested if Statement

You can also use an if statement inside another if statement, known as a nested if statement. The syntax is:

if (condition1) {
if (condition2) {
// code block 1
} else {
// code block 2
}
} else {
// code block 3
}

Example 4: Nested if…else Statement Output

Let’s use a nested if statement to check whether a given number is positive, negative, or zero:

let number = 5
if number > 0 {
if number > 10 {
print("Number is greater than 10")
} else {
print("Number is between 0 and 10")
}
} else if number < 0 {
print("Number is negative")
} else {
print("Number is zero")
}

In this example, we use a nested if statement to check whether the number is greater than 10, and if so, print a specific message. If not, we print another message. If the number is negative or zero, we execute the corresponding else blocks.

By mastering the three forms of the if...else statement and the nested if statement, you’ll be able to write more efficient and robust code in Swift.

Leave a Reply

Your email address will not be published. Required fields are marked *