美文网首页
Swift-流程控制for循环

Swift-流程控制for循环

作者: 守护地中海的花 | 来源:发表于2020-11-16 10:13 被阅读0次
    image.png

    区间

    • 开区间
      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
    

    相关文章

      网友评论

          本文标题:Swift-流程控制for循环

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