Control Flow
If Else
if, else if, and else choose one branch based on Boolean conditions.
Choose a branch
if_else.swift
let score =
var band = "needs practice"
if score >= 90 {
band = "excellent"
} else if score >= 70 {
band = "steady"
} else {
band = "needs practice"
}
print("score=\(score)")
print("band=\(band)")
let score =
var band = "needs practice"
if score >= 90 {
band = "excellent"
} else if score >= 70 {
band = "steady"
} else {
band = "needs practice"
}
print("score=\(score)")
print("band=\(band)")
let score =
var band = "needs practice"
if score >= 90 {
band = "excellent"
} else if score >= 70 {
band = "steady"
} else {
band = "needs practice"
}
print("score=\(score)")
print("band=\(band)")
branching
An `if` chain tests conditions in order. Swift runs the first branch whose condition is true and skips the rest.