Swift for 循环用来重复执行一系列语句直到达成特定条件,一般通过在每次循环完成后增加计数器的值来实现。
Swift for 循环的语法格式如下:
for init; condition; increment{
循环体
}
参数解析:
流程图:
import Cocoa
var someInts:[Int] = [10, 20, 30]
for var index = 0; index < 3; ++index {
print( "索引 [\(index)] 对应的值为 \(someInts[index])")
}
以上程序执行输出结果为:
索引 [0] 对应的值为 10
索引 [1] 对应的值为 20
索引 [2] 对应的值为 30