Welcome to the Swift if statement tutorial! In this guide, we'll learn how to make decisions in your Swift code using the if statement.
By the end of this tutorial, you'll be able to write your own Swift programs that make decisions based on conditions, opening up a world of possibilities for creating more intelligent and interactive applications.
if Statement 📝In Swift, the if statement allows you to test a condition and execute a block of code if the condition is true. This is the basic structure of an if statement:
if condition {
// Code to execute if the condition is true
}Let's break it down:
if condition: This is the condition that you want to test. If the condition evaluates to true, the code inside the curly braces will be executed.// Code to execute if the condition is true: This is the code that will be executed if the condition is true.Let's consider an example where we check if a number is greater than 10:
let number = 15
if number > 10 {
print("The number is greater than 10.")
}In this example, we have a variable number that holds the value of 15. We're testing if number is greater than 10. Since it is, the message "The number is greater than 10." will be printed to the console.
if Statement with Multiple Conditions 📝You can test multiple conditions in a single if statement using the && operator, which stands for "and." Here's the structure:
if condition1 && condition2 {
// Code to execute if both conditions are true
}Both conditions must be true for the code inside the curly braces to be executed. If either condition is false, the code will not be executed.
Let's modify our previous example to check if a number is both greater than 10 and less than 20:
let number = 15
if number > 10 && number < 20 {
print("The number is between 10 and 20.")
}In this example, we're testing if the number is both greater than 10 and less than 20. Since it is, the message "The number is between 10 and 20." will be printed to the console.
if Statement with Else 📝You can use the else keyword to specify a block of code to be executed if the condition in the if statement is false. Here's the structure:
if condition {
// Code to execute if the condition is true
} else {
// Code to execute if the condition is false
}Let's modify our previous example to print a message if the number is not between 10 and 20:
let number = 15
if number > 10 && number < 20 {
print("The number is between 10 and 20.")
} else {
print("The number is not between 10 and 20.")
}In this example, we're testing if the number is between 10 and 20. Since the number is already 15, the condition is false, and the message "The number is not between 10 and 20." will be printed to the console.
if Statement with Else if 📝You can use else if statements to test multiple conditions in a more complex way. Here's the structure:
if condition1 {
// Code to execute if condition1 is true
} else if condition2 {
// Code to execute if condition1 is false and condition2 is true
} else {
// Code to execute if none of the conditions are true
}You can add as many else if statements as you need. The first condition that evaluates to true will be executed.
Let's modify our previous examples to check if a number is between 1, 10, or 20:
let number = 15
if number > 20 {
print("The number is greater than 20.")
} else if number > 10 && number <= 20 {
print("The number is between 11 and 20.")
} else if number > 1 {
print("The number is between 2 and 10.")
} else {
print("The number is less than 2.")
}In this example, we're testing if the number is greater than 20, between 11 and 20, between 2 and 10, or less than 2. Since the number is 15, the condition number > 10 && number <= 20 is true, and the message "The number is between 11 and 20." will be printed to the console.
What does the `if` statement in Swift do?
What is the purpose of the `else` keyword in Swift?