- Swift Functional Programming(Second Edition)
- Dr. Fatih Nayebi
- 136字
- 2021-07-02 23:54:25
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
}
推薦閱讀
- 從零開始學(xué)Hadoop大數(shù)據(jù)分析(視頻教學(xué)版)
- Java Data Science Cookbook
- Python金融大數(shù)據(jù)分析(第2版)
- Learning JavaScriptMVC
- MySQL基礎(chǔ)教程
- Access 2016數(shù)據(jù)庫技術(shù)及應(yīng)用
- Mastering Machine Learning with R(Second Edition)
- 大數(shù)據(jù)Hadoop 3.X分布式處理實(shí)戰(zhàn)
- 基于Apache CXF構(gòu)建SOA應(yīng)用
- 基于OPAC日志的高校圖書館用戶信息需求與檢索行為研究
- 金融商業(yè)算法建模:基于Python和SAS
- HikariCP連接池實(shí)戰(zhàn)
- Construct 2 Game Development by Example
- Google Cloud Platform for Developers
- MySQL DBA修煉之道