Swift If (conditional) Statement
In Swift, the if
statement is used to execute code only when a certain condition is true. It is one of the most basic and important constructs in control flow.
Basic Syntax of the If Statement
The basic syntax of an if
statement in Swift is:
1if condition { 2 // code to be executed if the condition is true 3}
The code inside the braces will be executed only if the condition is true. If the condition is false, the code will be skipped.
Here is an example of using an if
statement to determine if a number is greater than zero:
1let number = 10 2if number > 0 { 3 print("The number is positive.") 4}
Output:
The number is positive.
Swift If Else Statement
In Swift, the if-else
statement is used to make decisions in the code based on some conditions. It allows the code to execute different blocks of code based on the result of a comparison or a condition.
If Else Syntax
The general syntax for the if-else
statement in Swift is:
1if condition { 2 // code to execute if condition is true 3} else { 4 // code to execute if condition is false 5}
The condition
is a boolean expression that is evaluated to either true
or false
. If the condition
is true
, the code inside the first block is executed. If the condition
is false
, the code inside the second block is executed.
Example
Let's take an example of a simple program that checks whether a person is eligible to vote or not based on their age.
1let age = 20 2 3if age >= 18 { 4 print("You are eligible to vote!") 5} else { 6 print("You are not eligible to vote yet.") 7}
In the above example, the if
statement checks whether the age
variable is greater than or equal to 18. If it is true, the code inside the first block is executed, which prints "You are eligible to vote!". If it is false, the code inside the second block is executed, which prints "You are not eligible to vote yet.".
Swift if-else-if Statement
In Swift, the if-else-if statement is used to check for multiple conditions and execute different code blocks based on each condition. This is achieved using the else if
keyword which is used to add additional conditions to the statement.
Here's the basic syntax of an if-else-if statement in Swift:
1if condition1 { 2 // code to be executed if condition1 is true 3} else if condition2 { 4 // code to be executed if condition2 is true 5} else if condition3 { 6 // code to be executed if condition3 is true 7} else { 8 // code to be executed if all conditions are false 9}
In this example, the conditions are evaluated in the order they are presented. If condition1
is true, the code block following the if
statement will be executed. If condition1
is false, condition2
will be evaluated. If condition2
is true, the code block following the first else if
statement will be executed. This process will continue until either one of the conditions is true or all conditions are false. If all conditions are false, the code block following the else
statement will be executed.
Let's see some examples of if-else-if statements in Swift.
Example 1: Checking the Value of a Variable
1let num = 10 2 3if num < 0 { 4 print("The number is negative") 5} else if num == 0 { 6 print("The number is zero") 7} else { 8 print("The number is positive") 9}
Output:
The number is positive
In this example, we have a variable num
with the value of 10
. We use the if-else-if statement to check whether the value of num
is negative, zero or positive. Since the value of num
is positive, the code block following the last else if
statement is executed and "The number is positive" is printed to the console.
Example 2: Checking the Length of a String
1let str = "Swift Programming" 2 3if str.count < 10 { 4 print("The string is too short") 5} else if str.count > 20 { 6 print("The string is too long") 7} else { 8 print("The string is just right") 9}
Output:
The string is just right
Example 3: Checking the User's Age
1let age = 25 2 3if age < 18 { 4 print("You are not eligible to vote") 5} else if age >= 18 && age < 21 { 6 print("You are eligible to vote, but cannot drink alcohol") 7} else if age >= 21 && age < 65 { 8 print("You are eligible to vote and drink alcohol") 9} else { 10 print("You are eligible to vote and drink alcohol, but please drink responsibly") 11}
Output:
You are eligible to vote and drink alcohol
Swift Nested if else Statement
In Swift programming, a nested if else statement is used to evaluate multiple conditions in a hierarchical order. In a nested if else statement, an if else statement is present inside another if else statement.
The syntax of a nested if else statement is as follows:
1if condition1 { 2 // code to execute when condition1 is true 3 if condition2 { 4 // code to execute when condition2 is true 5 } else { 6 // code to execute when condition2 is false 7 } 8} else { 9 // code to execute when condition1 is false 10}
Let's look at an example to understand how to use nested if else statements in Swift programming.
1let a = 20 2let b = 30 3let c = 40 4 5if a > b { 6 if a > c { 7 print("a is the largest number") 8 } else { 9 print("c is the largest number") 10 } 11} else { 12 if b > c { 13 print("b is the largest number") 14 } else { 15 print("c is the largest number") 16 } 17}
In this example, we have three variables a
, b
, and c
. We are using a nested if else statement to find the largest number among these three variables.
First, we are checking if a
is greater than b
. If this condition is true, we are checking if a
is greater than c
. If this condition is also true, we are printing "a is the largest number". If the condition a > c
is false, we are printing "c is the largest number".
If the condition a > b
is false, we are checking if b
is greater than c
. If this condition is true, we are printing "b is the largest number". If the condition b > c
is false, we are printing "c is the largest number".
When you run this program, the output will be:
c is the largest number
In this example, the value of c
is the largest among the three variables.
That's how you can use nested if else statements in Swift programming to evaluate multiple conditions in a hierarchical order.