区间
- 开区间
0..<3 ---》0 1 2 - 闭区间
0...3---》0 1 2 3
for循环
基础for循环
for i in 0...3 {
print(i)
}
let names = ["wpp","yuanli","mosi"]
for i in names {
print(i)
}
控制台:
0
1
2
3
wpp
yuanli
mosi
- i 默认是let
let names = ["wpp","yuanli","mosi"]
for i in 0..<names.count {
print(names[i])
}
for-range
let range = 0...3
for i in range {
print(i)
}
for-区间运算符用在数组上
let names = ["wpp","yuanli","mosi"]
print(names)
print(names[0..<3])
print(names[0...])
print(names[1...])
控制台:
["wpp", "yuanli", "mosi"]
["wpp", "yuanli", "mosi"]
["wpp", "yuanli", "mosi"]
["yuanli", "mosi"]
for name in names[0..<3] {
print(name)
}
控制台:
wpp
yuanli
mosi
网友评论