Skip to main content

Swift循环

实例 1

import Cocoa

for index in 1...5 {
print("\(index) 乘于 5 为:\(index * 5)")
}

例子中用来进行遍历的元素是使用闭区间操作符(...)表示的从1到5的数字区间。i

以上程序执行输出结果为:

1 乘于 5 为:5
2 乘于 5 为:10
3 乘于 5 为:15
4 乘于 5 为:20
5 乘于 5 为:25

实例 2

import Cocoa

var someInts:[Int] = [10, 20, 30]

for index in someInts {
print( "index 的值为 \(index)")
}

以上程序执行输出结果为:

index 的值为 10
index 的值为 20
index 的值为 30