Swift For in & repeat while 循环
2021-08-14 本文已影响0人
_发强
for in 循环字典
// for in 循环
let dict = ["zhangsan":40, "lisi": 43, "wangwu":42]
for item in dict {
print("\(item.key) is \(item.value)")
}
for (name,age) in dict {
print("\(name) is \(age)")
}
输出结果
zhangsan is 40
lisi is 43
wangwu is 42
for in 分段区间: 开区间
// for in 分段区间: 开区间 包含 from, 不包含 to
for i in stride(from: 0, to: 50, by: 5) {
print(i)
}
// 输出结果,0,5,10,15...45
for in 分段区间: 闭区间
// for in 分段区间: 闭区间 ,包含 from 和 through
for i in stride(from: 0, through: 50, by: 5) {
print(i)
}
// 输出结果,0,5,10,15...50
repeat while 循环
var count = 0
repeat {
print(count)
count += 1
} while count<5
输出结果:
0 1 2 3 4