官术网_书友最值得收藏!

for loops

Swift provides for and for-in loops. We can use the for-in loop to iterate over items in a collection, a sequence of numbers such as ranges, or characters in a string expression. The following example presents a for-in loop to iterate through all items in an Int array:

let scores = [65, 75, 92, 87, 68] 
var teamScore = 0

for score in scores {
if score > 70 {
teamScore = teamScore + 3
} else {
teamScore = teamScore + 1
}
}

and over dictionaries:

for (cheese, wine) in cheeseWinePairs{ 
print("\(cheese): \(wine)")
}

As C styles for loops with incrementers/decrementers are removed from Swift 3.0, it is recommended to use for-in loops with ranges instead, as follows:

var count = 0 
for i in 0...3 {
count + = i
}
主站蜘蛛池模板: 陵川县| 渝中区| 泾源县| 临海市| 巍山| 门源| 贺兰县| 汉阴县| 秦安县| 天门市| 淮阳县| 沂南县| 金山区| 安泽县| 福海县| 来宾市| 南安市| 西贡区| 汝阳县| 保康县| 台中市| 项城市| 三亚市| 泗洪县| 达孜县| 长岛县| 锦屏县| 射阳县| 阳山县| 若羌县| 绩溪县| 凤凰县| 祁阳县| 措勤县| 泽州县| 密山市| 诸暨市| 中方县| 麻城市| 汉川市| 东乡县|