美文网首页
3.Swift - 控制语句

3.Swift - 控制语句

作者: IOXusu | 来源:发表于2020-07-12 01:10 被阅读0次

    3.循环语句
    for

    let individualScores = [75, 43, 103, 87, 12]
    for score in individualScores {
        if score > 50 {
            print("\(score)")
        }
    }
    
    let interestingNumbers = [
        "Prime": [2, 3, 5, 7, 11, 13],
        "Fibonacci": [1, 1, 2, 3, 5, 8],
        "Square": [1, 4, 9, 16, 25],
    ]
    var largest = 0
    for (kind, numbers) in interestingNumbers {
        for number in numbers {
            if number > largest {
                largest = number
            }
        }
    }
    print(largest)
    > Prints "25"
    

    while

    var n = 2
    while n < 100 {
        n *= 2
    }
    print(n)
    

    repeat while

    var m = 2
    repeat {
        m *= 2
    } while m < 100
    print(m)
    

    for in ...

    var total = 0
    //注意 ... 包含左右  ..< 包左不包右
    for i in 0..<4 {
        total += i
    }
    print(total)
    

    相关文章

      网友评论

          本文标题:3.Swift - 控制语句

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