Swift Tutorial
Swift Data Types
Swift Control Flow
Swift Functions
Swift Collections
Swift OOPs
Swift Additional Topics
The if-else-if
statement in Swift is used for making decisions in code based on whether specific conditions are true or false. You can test multiple conditions in sequence, and when a true condition is found, the associated block of code is executed. If none of the conditions are true, you can provide an optional else
block to execute.
Here's the general structure of an if-else-if
statement:
if condition1 { // Code to execute if condition1 is true } else if condition2 { // Code to execute if condition1 is false and condition2 is true } else if condition3 { // Code to execute if condition1 and condition2 are false, but condition3 is true } else { // Code to execute if none of the above conditions are true }
Let's say you have a score and you want to assign a grade based on the score:
let score = 85 if score >= 90 { print("A grade") } else if score >= 80 { print("B grade") } else if score >= 70 { print("C grade") } else if score >= 60 { print("D grade") } else { print("F grade") } // Output: B grade
Here's how the program works:
else
block executes, printing "F grade".You can have as many else if
conditions as you need. Also, the final else
block is optional. If it's omitted and none of the conditions are true, then no action will be taken.
Remember to ensure the order of conditions makes sense. In the example above, if we tested for a score of 80 or above before testing for 90 or above, everyone with a score of 90 or more would also receive a "B grade".
How to use if-else-if in Swift programming:
Description: The if-else-if
statement allows you to evaluate multiple conditions sequentially until one of them is true.
Code:
let number = 10 if number > 0 { print("Number is positive") } else if number < 0 { print("Number is negative") } else { print("Number is zero") }
Nested if-else-if statements in Swift:
Description: You can nest if-else-if
statements to handle more complex conditions.
Code:
let grade = 85 if grade >= 90 { print("A") } else { if grade >= 80 { print("B") } else { print("C") } }
Swift multiple conditions in if-else-if:
Description: You can use multiple conditions in if-else-if
statements to handle various cases.
Code:
let temperature = 25 if temperature > 30 { print("Hot") } else if temperature > 20 && temperature <= 30 { print("Moderate") } else { print("Cold") }
Chaining if-else-if in Swift:
Description: You can chain multiple if-else-if
statements for a cleaner and more readable code.
Code:
let marks = 75 if marks >= 90 { print("A") } else if marks >= 80 { print("B") } else if marks >= 70 { print("C") } else { print("D") }
Swift switch statement vs if-else-if:
Description: switch
statements provide an alternative to multiple if-else-if
conditions, offering a more concise syntax.
Code:
let dayOfWeek = "Monday" switch dayOfWeek { case "Monday", "Tuesday", "Wednesday", "Thursday", "Friday": print("Weekday") case "Saturday", "Sunday": print("Weekend") default: print("Invalid day") }
Evaluating ranges with if-else-if in Swift:
Description: You can use ranges in if-else-if
statements for evaluating numeric or other types of ranges.
Code:
let score = 75 if score >= 90 { print("A") } else if score >= 80 && score < 90 { print("B") } else if score >= 70 && score < 80 { print("C") } else { print("D") }
Combining logical operators in if-else-if statements:
Description: Logical operators (&&
, ||
, !
) can be combined to create more complex conditions in if-else-if
statements.
Code:
let age = 25 let hasLicense = true if age >= 18 && hasLicense { print("Can drive") } else if age >= 16 { print("Can get a learner's permit") } else { print("Too young to drive") }
Using else if with optional binding in Swift:
Description: Optional binding with if-let
can be used in else if
clauses to safely unwrap and use optional values.
Code:
let optionalValue: Int? = 42 if let unwrappedValue = optionalValue { print("Optional has a value: \(unwrappedValue)") } else { print("Optional is nil") }