swift基础day3

1.for循环与while循环(Control Flow: For-In Loops, While Loops)

//for循环
for index in 1...3{
    print("\(index) times 6 is \(index * 6)")
}
let num1 = 0
let num2 = 20
for index in stride(from: num1, to: num2, by: 5){
    print(index) //0,5,10,15不包含20
} //stride(...to...)前闭后开区间
for index in stride(from: num1, through: num2, by: 10){
    print(index)  //10,20。包含20
}  //stride(...through..)前闭后闭区间
//while循环  掷骰子走步数
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
//while语句
while square < finalSquare {
    diceRoll += 1
    if diceRoll == 7{
        diceRoll = 1
    }
    square = square + diceRoll
    if square < board.count{
        square += board[square]
    }
}
print("Game over!")
repeat{
    square += board[square]
    diceRoll += 1
    if diceRoll == 7{
        diceRoll = 1
    }
    square += diceRoll
}while square < finalSquare

2.if语句与switch语句(Conditional Statements: if, switch)

//if语句。不需要完整判断时  不会打印任何
let temperatuFahrenheit = 72
if temperatuFahrenheit  <= 32{
    print("It's very cold. Consider wearing a scarf.")
}else if temperatuFahrenheit >= 86{
    print("It's really warm. Don't forget to wear sunscreen.")
}
//switch复合匹配
let anotherCharacter: Character = "a"
switch anotherCharacter {
case "a","A":
    print("The letter is A")
default:
    print("The letter is not A")
}
//swich 区间匹配
let approximateCount = 62
let countedThings = "moon 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"
default:
    naturalcount = "many"
}
print("There are \(naturalcount) \(countedThings)")
//元组匹配
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")
}

值绑定:…case let(x, y):…
where:给case增加额外的条件
复合型case:“,”分隔
复合匹配:复合匹配可以包含值绑定。复合匹配里所有的匹配模式,都必须包含相同的值绑定。并且每一个绑定都必须获取到相同类型的值。
3.控制转移语句(Control Transfer Statements): Continue, break, fallthrough,Labeled Statements, return,throw
continue:循环体立刻停止本次循环,重新开始下次循环

let puzzleInput = "great minds think alike"
var puzzleOutput = ""
for character in puzzleInput{
    switch character {
    case "a", "e", "i", "o", "u", " ":
        continue
    default:
        puzzleOutput.append(character)
    }
}
print(puzzleOutput)

break:立刻结束整个控制流的执行

let numberSymbol: Character = "五"
var possibleIntergerValue: Int?
switch numberSymbol {
case "1", "١", "一", "๑":
    possibleIntergerValue = 1
case "2", "٢", "二", "๒":
    possibleIntergerValue = 2
case "3", "٣", "三", "๓":
    possibleIntergerValue = 3
case "4", "٤", "四", "๔":
    possibleIntergerValue = 4
default:
    break
}
if let intergerValue = possibleIntergerValue {
    print("The interger value of \(numberSymbol) is \(intergerValue)")
}else{
    print("An integer value could not be found for \(numberSymbol).")
}

贯穿:fallthrough 继续向下执行

let integerToDescribe = 5
var description = "The number \(integerToDescribe) is"
switch integerToDescribe {
case 2, 3, 5, 7, 11, 13, 17, 19:
    description += " a prime number,and also"
    fallthrough
case  4, 6, 8, 10:
    description += " 4, 6, 8, 10 is not zhishu"
    fallthrough
default:
    description += " an integer."
}
print(description)

带标签的语句:label

//会循环很多次,运行时请慎重
let finalSquare1 = 25
var board2 = [Int](repeating : 0, count: finalSquare1 + 1)
gameLoop: while square != finalSquare1{
    diceRoll += 1
    if diceRoll == 7{
        diceRoll = 1
    }
    switch square + diceRoll {
    case finalSquare1:
        break gameLoop
    case let newSquare where newSquare > finalSquare1:
        continue gameLoop
    default:
        square += diceRoll
        square += board[square]
    }
}
print("Game over!")

提前退出:使用 guard 语句来要求条件必须为真时,以执行 guard 语句后的代码,个 guard 语句总是有一个 else 从句,如果条件不为真则执行 else 从句中的代码

func greet(person: [String: String]){
    guard let name = person["name"] else {
        return
    }
    print("Hello \(name) !")
    guard let location = person["location"] else {
        print("I hope the weather is nice near you.")
                return
    }
    print("I hope the weather is nice in \(location).")
}
greet(person: ["name" : "John"]) //Hello John!  I hope the weather is nice near you.
greet(person: ["name" : "John", "location": "Cupertino"])//Hello John !  I hope the weather is nice in Cupertino.
I hope the weather is nice in Cupertino.

检测API是否可用:if或guard语句 #available

if #available(iOS 10, macOS 10.12, *){
    print("yes")
}else{
    print("No")
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值