03-Swift 循环的使用

一、for 循环的使用

  • 用法一:
//MARK : for 循环语句
for i in 1..<5{
    print(i)
}
  • 用法二:
//打印两遍 test,此时不需要创建变量 i
for _ in 1...2{
    print("test")
}

二、while 循环的使用

  • while 的一般使用
//MARK : while 循环
var i = 3
while i>0 {
    print(i)
    i-=1;
}
  • do...while 循环的使用
i = 10
repeat{
    print(i)
    i-=1
}while i>0
  • Swift3.0 中,do 为捕捉异常的关键字,而循环中的 do 改为 repeat。

你可能感兴趣的:(03-Swift 循环的使用)