Swift 2 学习笔记 3.逻辑控制

课程来自慕课网liuyubobobo老师


逻辑控制
  • 循环结构
    • for in 循环
    for i in 1..<10{
        print(i)  // 1~9
    }
    
    var resule = 1
    var base = 2
    for _ in 1...10{
        resule *= base
    }
    resule  // 1024
    
    • while 循环
    // a b扔骰子,谁先连赢三局
    var aWin = 0
    var bWin = 0
    var game = 0
    
    while aWin < 3 && bWin < 3 {
        game += 1
    
        let a = arc4random_uniform(6) + 1
        let b = arc4random_uniform(6) + 1
    
        if a > b {
            print("a Win")
            aWin += 1
            bWin = 0
        }else if b > a {
            print("b Win")
            bWin += 1
            aWin = 0
        }else{
            print("Draw")
            aWin = 0
            bWin = 0
        }
    }
    
    let winner = aWin == 3 ? "a" : "b"
    print("Winner is \(winner)")
    
    • repeat while 循环
    // a b扔骰子,谁先连赢三局
    var aWin = 0
    var bWin = 0
    var game = 0
    
    repeat{
        game += 1
    
        let a = arc4random_uniform(6) + 1
        let b = arc4random_uniform(6) + 1
    
        if a > b {
            print("a Win")
            aWin += 1
            bWin = 0
        }else if b > a {
            print("b Win")
            bWin += 1
            aWin = 0
        }else{
            print("Draw")
            aWin = 0
            bWin = 0
        }
    }while aWin < 3 && bWin < 3
    
    let winner = aWin == 3 ? "a" : "b"
    print("Winner is \(winner)")
    
    • 循环控制
    // a和b掷骰子直到分出胜负
    while true {
        let a = arc4random_uniform(6)+1
        let b = arc4random_uniform(6)+1
    
        if a == b {
            print("draw")
            continue  // 平局 再比一次
        }
    
        let winner = a > b ? "a" : "b"
        print("winner is \(winner)")
        break  // 分出胜负 跳出循环
    }
    
  • 选择结构
    • if 结构
    let passWord = "jilei"
    if passWord == "jilei" {
        print("Pass")
    }else{
        print("Error")
    }
    
    • switch 结构
    // switch 语句必须穷举全部可能性
    let rating = "A"
    switch rating{
    case "A":
        print("Great!")
    case "B":
        print("Just so so.")
    case "C":
        print("It's bad!")
    default:
        break
    }
    
    • switch 的更多用法
    case "A","a":  // 多个匹配值
    case 1..<60:  // 范围匹配值
    // 元组
    case (0,0):  
    case (_,0):  // 忽略元组中第一个值
    case (let x,0):  // 忽略元组中第一个值,之后需要使用
    case (-1...1,-1...1):
    
    // fallthrough 允许跳入下一个case
    let x = (0,0)
    
    switch x {
    case (0,0):
        print("It's origin!")
        fallthrough
    case (_,0):
        print("It's on the x-axis.")
        fallthrough
    case (0,_):
        print("It's on the y-axis.")
    default:
        print("It's just an ordinary point")
    }
    // It's origin!"
    // It's on the x-axis.
    // It's on the y-axis.
    
    • 控制转移
    // x^4 - y^2 = 15xy 求一个300内的整数解
    findAnswer:
    for x in 1...300 {
        for y in 1...300 {
            if x*x*x*x - y*y == 15*x*y {
                print(x,y)
                break findAnswer  //跳出findAnswer循环
            }
        }
    }
    // 4 4
    
    • where 与 模式匹配
    // 点在 x=y 直线上
    case let (x,y) where x == y:
    
    // Swift3 使用 , 代替 where 进行约束
    let age = 19
    if case 10...19 = age, age >= 18 {
        print(age)
    }
    
    //1到100之内能被3整除的整数
    for case let i in 1...100 where i%3==0 {
        print(i)
    }
    
    • guard 与 代码风格初探
    func buy(money: Int, price: Int, capacity: Int, volume: Int){
        guard money >= price else{
            print("Not enough money")
            return
        }
        guard capacity >= volume else{
            print("Not enough capacity")
            return
        }
        print("Buy it!")
    }
    

你可能感兴趣的:(Swift 2 学习笔记 3.逻辑控制)