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

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
}
主站蜘蛛池模板: 抚顺市| 大余县| 吉安市| 疏勒县| 历史| 于田县| 柘荣县| 柏乡县| 嵩明县| 桦甸市| 陆川县| 临泉县| 牟定县| 怀宁县| 定边县| 怀宁县| 株洲市| 巴里| 宜黄县| 板桥市| 天镇县| 汝阳县| 仁布县| 明水县| 明光市| 金溪县| 尉氏县| 枝江市| 中方县| 通江县| 宁陕县| 黔江区| 德保县| 虹口区| 苏尼特右旗| 保康县| 武乡县| 雅江县| 新田县| 建平县| 昭觉县|