Swift2.2 学习笔记(十二) ___控制流

For-In

for index in 1...5 {
    //闭区间操作符(...)
    print("\(index) times 5 is \(index * 5)")
    //注意index 常量只存在于循环的生命周期里。
}
/*
1 times 5 is 5
2 times 5 is 10
3 times 5 is 15
4 times 5 is 20
5 times 5 is 25
*/

如果不需要知道范围内的每一项的值,可以使用下划线(_)替代变量名来忽略对值的访问

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

//3 to the power of 10 is59049

使用for-in遍历一个数组所有元素

let names = ["Anna","Alex","Brian","Jack"]
for name in names {
    print("Hello,\(name)!")
}

let numberOfLegs = ["spider":8,"ant":6,"cat":4]
for (animalName,legCount) in numberOfLegs {
    print("\(animalName)s have \(legCount) legs")
}
let numberOfLegs = ["spider": 8, "ant": 6, "cat": 4]
for (animalName, legCount) in numberOfLegs {
    print("\(animalName)s have \(legCount) legs")
}
// ants have 6 legs
// cats have 4 legs
// spiders have 8 legs

For条件递增 (for-condition-increment)

for var index = 0; index < 3; ++index {
    print("index is \(index)")
}

var index:Int
for index = 0; index < 3; ++index {
    print("index is \(index)")
}
print("The loop statements were executed \(index) times")

while 循环
while condition {
statements
}

let finalSqure = 25
var board = [Int](count: finalSqure + 1, repeatedValue: 0)
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 square < finalSqure {

    if ++diceRoll == 7 { diceRoll = 1 }

    square += diceRoll

    if square < board.count {

        square += board[square]
    }
}
print("GameOver!")

Do-While
do {
statements
} while condition

//条件语句
//If
var temperatureInFahrenheit = 30
if temperatureInFahrenheit <= 32 {
    print("it's very cold. Consider wearing a scarf")
}

// Switch
let somCharacter:Character = "e"
switch somCharacter {
    case "a","b":
    print("1")
    case "c","d","e":
    print("2")
    default:
    print("nothing")
}

元祖,在同一个switch语句中测试多个值。元组中的元素可以是值,也可以是范围。使用(_)来匹配所有可能的值

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

    //    (1,1) is inside the box

}

值绑定
case块的模式允许将匹配的值绑定到一个临时的常量或者变量,这些常量或变量在该case块里就可以被引用了

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("somewhee else at (\(x),\(y))")

    // on the x-axis with an x value of 2
}

Where
case块的模式可以使用where语句来判断额外的条件

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 linx x == -y)")
case let (x,y):
    print("(\(x),\(y) is just some arbirthary point)")

    //    (1,-1 is on the linx x == -y)


}

Swift四种控制转移语句
-continue
-break
-fallthrough
-return

//continue 语句告诉循环停止正在做的事情并且再次从开始循环的下一次遍历。就是说“我不再继续当前的循环遍历了”而不是离开整个的循环
let puzzleInput = "great minds think alike"
var puzzleOutput = ""
for character in puzzleInput.characters {
    switch character {
        case "a" ,"e","i","o","u":
        continue
    default:
        puzzleOutput.append(character)
    }
}
print(puzzleOutput)  //grt mnds thnk lk

Break 立即结束整个控制流语句。
当在switch语句里使用时,break 导致 switch 语句立即结束它的执行,并且转移控制到 switch 语句结束花括号( })之后的第一行代码上

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

Fallthrough
如果你确实需要 C 风格的贯穿行为,你可以选择在每个情况末尾使用 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
//   使用 fallthrough 关键字来“贯穿到” default 情况。 default 情况添加额外的文字到描述的末尾,接着 switch 语句结束。
default:
    description += "an integer."
}
print(description)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值