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

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
}
主站蜘蛛池模板: 民丰县| 井研县| 海伦市| 宝山区| 敖汉旗| 繁峙县| 延寿县| 广丰县| 荣成市| 常熟市| 云和县| 武平县| 界首市| 葫芦岛市| 丽江市| 新邵县| 东辽县| 囊谦县| 临颍县| 探索| 汝州市| 闸北区| 鱼台县| 连平县| 连山| 鹰潭市| 长宁县| 古田县| 潮安县| 万安县| 永顺县| 桑日县| 新竹市| 原平市| 桓台县| 昌邑市| 桐庐县| 苍山县| 弋阳县| 长春市| 敖汉旗|