Introduction
The if-else statement is the most basic form of conditional branching in Swift. It lets a program execute one block of code when a condition is true and another block when it is false. Unlike C, Objective-C, or JavaScript, Swift requires the condition inside an if statement to be a genuine Bool value — there is no automatic conversion from integers or other types to true/false. This design choice removes an entire class of bugs caused by accidentally writing if (x = 1) or relying on nonzero numbers being 'truthy'.
Cricket analogy: Unlike some scoring apps that treat any nonzero run count as 'out,' Swift's if statement requires a genuine Bool like 'isOut,' never auto-converting a run total, preventing bugs from misreading a score as a dismissal.
Syntax
if condition {
// executed when condition is true
} else if anotherCondition {
// executed when anotherCondition is true
} else {
// executed when all conditions are false
}Explanation
Parentheses around the condition are optional and idiomatic Swift omits them, but the curly braces around each branch are always mandatory, even for a single statement. The condition and anotherCondition expressions must evaluate to a Bool — writing if x where x is an Int is a compile-time error. You can chain as many else if clauses as needed, and the final else is optional; if omitted and no condition matches, control simply falls through to the code after the statement.
Cricket analogy: Just as an umpire's signal for six must be a full raised-arms gesture, no shortcuts, Swift's curly braces around each if branch are mandatory even for one line, while parentheses around 'isSix' are optional; you can chain else-if for four, out, or wide.
Example
let temperature = 28
if temperature > 30 {
print("It's hot outside.")
} else if temperature > 20 {
print("It's warm outside.")
} else {
print("It's cold outside.")
}Output
It's warm outside.Key Takeaways
- The condition in an if statement must be a Bool — Swift has no implicit truthy/falsy conversion.
- Parentheses around the condition are optional; curly braces around each branch are mandatory.
- else if chains let you test multiple conditions in order, top to bottom.
- The trailing else clause is optional and runs only if no prior condition matched.
- if can also be used as an expression in Swift 5.9+ to directly produce a value.
Practice what you learned
1. What type must the condition in a Swift if statement evaluate to?
2. Which part of an if statement is mandatory in Swift?
3. What happens if you write `if 1 { ... }` in Swift?
4. Is the else clause required in a Swift if-else statement?
Was this page helpful?
You May Also Like
switch Statement in Swift
Explore Swift's powerful switch statement, which requires exhaustiveness and never falls through by default.
The guard Statement in Swift
The guard statement performs early-exit validation, unwrapping optionals for use in the rest of the enclosing scope.
Optional Binding in Swift
Optional binding safely unwraps an optional's value into a local constant only when a value is actually present.
Operators in Swift
Understand Swift's arithmetic, comparison, logical, and range operators, and how the closed and half-open range operators differ.
Related Reading
Related Study Notes in Programming
Browse all study notesApache Spark Study Notes
Programming · 30 topics
ProgrammingApache Flink Study Notes
Programming · 30 topics
ProgrammingHadoop Study Notes
Programming · 30 topics
ProgrammingSnowflake Study Notes
Programming · 30 topics
ProgrammingApache Airflow Study Notes
Programming · 30 topics
Programmingdbt (Data Build Tool) Study Notes
Programming · 30 topics