第六天:循环

https://www.hackingwithswift.com/100/swiftui/6

for in

let platforms = ["iOS", "macOS", "tvOS", "watchOS"]

for os in platforms {
    print("Swift works great on \(os).")
}

for i in 1...12 {
    print("5 x \(i) is \(5 * i)")
}

for i in 1..<5 {
    print("Counting 1 up to 5: \(i)")
}

for _ in 1...5 {
    lyric += " hate"
}

1...12是包含1和12的ClosedRange1..<5是不包含5的Range

如果需要特定的step,使用stride函数。

for radians in stride(from: 0.0, to: .pi * 2, by: .pi / 2) {
    let degrees = Int(radians * 180 / .pi)
    print("Degrees: \(degrees), radians: \(radians)")
}

while

var countdown = 10

while countdown > 0 {
    print("\(countdown)…")
    countdown -= 1
}
print("Blast off!")

break & continue

break退出循环;continue跳过本次循环