当前位置: 代码迷 >> 综合 >> swift 循环for in高级用法 分段区间 streed from to by through 、while
  详细解决方案

swift 循环for in高级用法 分段区间 streed from to by through 、while

热度:55   发布时间:2024-02-13 19:47:02.0

使用stride(from:to:by:)函数来太哦过不想要的标记

闭区间也同样适用,使用stride(from:through:by:)即可

code

//: A UIKit based Playground for presenting user interfaceimport UIKitlet minuteInterval = 5
for tickMark in stride(from: 0, to: 50, by: minuteInterval){print(tickMark)
}print("---")
for tickMark in stride(from: 0, through: 50, by: minuteInterval){print(tickMark)
}

结果

0
5
10
15
20
25
30
35
40
45
---
0
5
10
15
20
25
30
35
40
45
50

结论 to 为开区间。throuugh是闭区间。by 是步长

 

repeat-while 循环

类似java的do while

code

//: A UIKit based Playground for presenting user interfaceimport UIKit
var count = 0
repeat{print(count)count+=1
} while count < 5
count = 0
repeat{print(count)count+=1
} while count < 0

结果

0

1

2

3

4

0