05-Swift循环

Swift循环

循环的介绍
  • 在开发中经常会需要循环
  • 常见的循环有:for/while/do while.
  • 这里我们只介绍for/while,因为for/while最常见
1 for循环的写法
  • 最常规写法 // 传统写法 for var i = 0; i < 10; i++ { print(i) }

  • 区间for循环 for i in 0..<10 { print(i) }

       for i in 0...10 {
       print(i)
       }
    
  • 特殊写法

    • 如果在for循环中不需要用到下标i for _ in 0..<10 { print("hello") }
2 while和do while循环
  • while循环
    • while的判断句必须有正确的真假,没有非0即真
    • while后面的()可以省略 var a = 0 while a < 10 { a++ }
  • do while循环
    • 使用repeat关键字来代替了do let b = 0 repeat { print(b) b++ } while b < 20

转载于:https://my.oschina.net/iOSliuhui/blog/728096

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值