美文网首页Swift 专栏
4.循环有:for/while/do while

4.循环有:for/while/do while

作者: IIronMan | 来源:发表于2017-04-26 15:43 被阅读24次
    • 1.for/while是最常见的两种循环

    • 2.for循环的写法

      • 1.最常规写法

        传统写法
        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循环
        特殊写法
        如果在`for`循环中不需要用到下标  `i`
          for _ in 0..<10 {
              print("hello")
          }
    
    • 3.whiledo while循环

      • while循环
        while的判断句必须有正确的真假,没有非0即真
        while后面的()可以省略

        var a = 0
        while a < 10 {
              a += 1
        }
        print(a)
        
      • do while循环 使用repeat关键字来代替了do
        使用repeat关键字来代替了do

        var b = 0
        repeat {
            print(b)
            b += 1
        } while b < 20

    相关文章

      网友评论

        本文标题:4.循环有:for/while/do while

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