Control Flow

作者: 宋奕Ekis | 来源:发表于2021-07-28 15:48 被阅读0次

    For-In Loops

    If we don’t need each value from a sequence, we can just ignore them by using an underscore(__) in place of a variable name.

    let base = 3
    let power = 10
    var answer = 1
    for _ in 1...power {
        answer *= base
    }
    print("\(base) to the power of \(power) is \(answer)")
    // Prints "3 to the power of 10 is 59049"
    

    We can use this function stride to skip some values at a step.

    let minutes = 60
    let minuteInterval = 5
    for tickMark in stride(from: 0, to: minutes, by: minuteInterval) {
        // render the tick mark every 5 minutes (0, 5, 10, 15 ... 45, 50, 55)
    }
    //stride(from:to:by:) for a range is not closed.
    
    let hours = 12
    let hourInterval = 3
    for tickMark in stride(from: 3, through: hours, by: hourInterval) {
        // render the tick mark every 3 hours (3, 6, 9, 12)
    }
    //stride(from:through:by:) for closed range
    

    While Loops

    Do the statements untile the condition is false.

    First is the condition and then do the statements.

    while condition {
        statements
    }
    

    Do the statements at start, then considering the condition.

    repeat {
        statements
    } while condition
    

    Conditional Statements

    Switch

    No Implicit Fallthrough

    In Swift’s switch statements, we don’t have to add break statement, there is a implicit break, but we must body code in each case.

    If we want to fallthrough several case, we just need to combine the values in a single case.

    let anotherCharacter: Character = "a"
    switch anotherCharacter {
    case "a", "A":
        print("The letter A")
    case "b": // Invalid, the case has an empty body
    default:
        print("Not the letter A")
    }
    

    Interval Matching

    We can use a range to let the case make sense, which is wonderful.

    let approximateCount = 62
    let countedThings = "moons orbiting Saturn"
    let naturalCount: String
    switch approximateCount {
    case 0:
        naturalCount = "no"
    case 1..<5:
        naturalCount = "a few"
    case 5..<12:
        naturalCount = "several"
    case 12..<100:
        naturalCount = "dozens of"
    case 100..<1000:
        naturalCount = "hundreds of"
    default:
        naturalCount = "many"
    }
    print("There are \(naturalCount) \(countedThings).")
    // Prints "There are dozens of moons orbiting Saturn."
    

    Tuples

    The tuple value can be used as case condition else.

    let somePoint = (1, 1)
    switch somePoint {
    case (0, 0):
        print("\(somePoint) is at the origin")
    case (_, 0):
        print("\(somePoint) is on the x-axis")
    case (0, _):
        print("\(somePoint) is on the y-axis")
    case (-2...2, -2...2):
        print("\(somePoint) is inside the box")
    default:
        print("\(somePoint) is outside of the box")
    }
    // Prints "(1, 1) is inside the box"
    

    Value Bindings

    Otherwise, we can name a value in case, so that we can access this value by the name.

    let anotherPoint = (2, 0)
    switch anotherPoint {
    case (let x, 0):
        print("on the x-axis with an x value of \(x)")
    case (0, let y):
        print("on the y-axis with a y value of \(y)")
    case let (x, y):
        print("somewhere else at (\(x), \(y))")
    }
    // Prints "on the x-axis with an x value of 2"
    

    Where

    This usage is confusing for me actually, we can use the where clause after case statement for additional conditions.

    let yetAnotherPoint = (1, -1)
    switch yetAnotherPoint {
    case let (x, y) where x == y:
        print("(\(x), \(y)) is on the line x == y")
    case let (x, y) where x == -y:
        print("(\(x), \(y)) is on the line x == -y")
    case let (x, y):
        print("(\(x), \(y)) is just some arbitrary point")
    }
    // Prints "(1, -1) is on the line x == -y"
    

    Labeled Statements

    We can name a loops or conditional statements with a label, then you can just call the statement by this name.

    let finalSquare = 25
    var board = [Int](repeating: 0, count: finalSquare + 1)
    board[03] = +08; board[06] = +11; board[09] = +09; board[10] = +02
    board[14] = -10; board[19] = -11; board[22] = -02; board[24] = -08
    var square = 0
    var diceRoll = 0
    
    gameLoop: while square != finalSquare {
        diceRoll += 1
        if diceRoll == 7 { diceRoll = 1 }
        switch square + diceRoll {
        case finalSquare:
            // diceRoll will move us to the final square, so the game is over
            break gameLoop
        case let newSquare where newSquare > finalSquare:
            // diceRoll will move us beyond the final square, so roll again
            continue gameLoop
        default:
            // this is a valid move, so find out its effect
            square += diceRoll
            square += board[square]
        }
    }
    print("Game over!")
    

    In above example, if we don't use the label to name the while statement, then calling the break or continue in switch will just execute within swift, but we can control the while flow directly by calling break or continue with it’s label.

    Let’s think!

    相关文章

      网友评论

        本文标题:Control Flow

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