控制流 / 循环

var sum10 = 0
//C-style for statement is deprecated
for i in 1...10 {
    sum10 += i
}
//sum10 is 55

print("sum10 is \(sum10)")


You use the for-in loop to iterate over a sequence, such as items in an array, ranges of numbers, or characters in a string.

You can also iterate over a dictionary to access its key-value pairs. Each item in the dictionary is returned as a (key, value) tuple when the dictionary is iterated, and you can decompose the (key, value) tuple’s members as explicitly named constants for use within the body of the for-in loop.