Swift If Else
Making decisions in your Swift programs
🤔 What are If Else Statements?
If else statements allow your program to make decisions and execute different code based on conditions. They're like asking questions and taking different actions based on the answers.
let temperature = 25
if temperature > 20 {
print("It's warm outside!")
} else {
print("It's cool outside!")
}
Output:
It's warm outside!
Conditional Logic Concepts
If Statement
Execute code when condition is true
if age >= 18 {
print("Adult")
}
Else Statement
Execute code when condition is false
if score >= 60 {
print("Pass")
} else {
print("Fail")
}
Else If
Check multiple conditions
if grade >= 90 {
print("A")
} else if grade >= 80 {
print("B")
}
Conditions
Boolean expressions that evaluate to true/false
let isRaining = true
if isRaining {
print("Take umbrella")
}
🔹 Basic If Statement
The simplest form checks one condition:
let age = 16
if age >= 18 {
print("You can vote!")
}
if age < 18 {
print("You're not old enough to vote yet.")
}
// Using boolean variables
let hasLicense = true
if hasLicense {
print("You can drive!")
}
Output:
You're not old enough to vote yet.
You can drive!
🔹 If Else Statement
Handle both true and false conditions:
let score = 85
if score >= 60 {
print("Congratulations! You passed!")
} else {
print("Sorry, you need to retake the test.")
}
// Weather example
let isRaining = false
if isRaining {
print("Take an umbrella!")
} else {
print("Enjoy the sunshine!")
}
// Number comparison
let number = 7
if number % 2 == 0 {
print("\(number) is even")
} else {
print("\(number) is odd")
}
Output:
Congratulations! You passed!
Enjoy the sunshine!
7 is odd
🔹 Else If for Multiple Conditions
Check multiple conditions in sequence:
let grade = 87
if grade >= 90 {
print("Grade: A - Excellent!")
} else if grade >= 80 {
print("Grade: B - Good job!")
} else if grade >= 70 {
print("Grade: C - Satisfactory")
} else if grade >= 60 {
print("Grade: D - Needs improvement")
} else {
print("Grade: F - Please retake")
}
// Temperature example
let temperature = 15
if temperature > 30 {
print("It's hot! Stay hydrated.")
} else if temperature > 20 {
print("Perfect weather!")
} else if temperature > 10 {
print("A bit cool, wear a jacket.")
} else {
print("It's cold! Bundle up!")
Output:
Grade: B - Good job!
A bit cool, wear a jacket.
🔹 Comparison Operators
Common operators used in conditions:
- == Equal to
- != Not equal to
- > Greater than
- < Less than
- >= Greater than or equal
- <= Less than or equal
let userAge = 25
let minimumAge = 21
if userAge == minimumAge {
print("Exactly the minimum age!")
} else if userAge > minimumAge {
print("Above minimum age")
} else {
print("Below minimum age")
}
// String comparison
let password = "secret123"
if password == "secret123" {
print("Access granted!")
} else {
print("Access denied!")
}
Output:
Above minimum age
Access granted!
🔹 Logical Operators
Combine multiple conditions:
let age = 25
let hasJob = true
let creditScore = 750
// AND operator (&&) - both conditions must be true
if age >= 18 && hasJob {
print("Eligible for credit card")
}
// OR operator (||) - at least one condition must be true
if age >= 65 || creditScore >= 800 {
print("Qualifies for premium rate")
}
// NOT operator (!) - reverses the condition
let isWeekend = false
if !isWeekend {
print("It's a weekday - time to work!")
}
// Complex condition
if (age >= 21 && hasJob) || creditScore >= 700 {
print("Approved for loan")
}
Output:
Eligible for credit card
It's a weekday - time to work!
Approved for loan