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

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
}
主站蜘蛛池模板: 连平县| 康保县| 三穗县| 比如县| 景德镇市| 渝中区| 康马县| 双城市| 同心县| 胶州市| 阿勒泰市| 隆回县| 新干县| 贡觉县| 久治县| 奉化市| 施秉县| 交口县| 江达县| 吉安市| 香港 | 九龙坡区| 徐州市| 宜都市| 忻城县| 鲁甸县| 栾城县| 平定县| 额尔古纳市| 保德县| 普陀区| 汾西县| 恩平市| 应用必备| 建平县| 大城县| 普安县| 梨树县| 兰州市| 武胜县| 沅江市|