美文网首页
Swift For in & repeat while 循环

Swift For in & repeat while 循环

作者: _发强 | 来源:发表于2021-08-14 00:00 被阅读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

相关文章

网友评论

      本文标题:Swift For in & repeat while 循环

      本文链接:https://www.haomeiwen.com/subject/pocsbltx.html