Swift 3.0学习笔记_8_控制流

包含 for-in, while, repeat-while, if, switch 的知识点

//控制流
//1.for-in 可遍历一个集合中的所有元素,例如数字范围,数组中的元素或者字符串中的字符.
for index in 1...5 { //...是闭区间, ..<表示半闭区间
    print("\(index)")
}
//index 是一个每次循环遍历开始时被自动赋值的常量,这种情况下, index 在使用前不需要声明,只需要将它包含在循环的声明中,就可以对其进行隐式声明,而无需使用 let 关键字声明.
//若不需要区间序列内每一项的值,你可以使用下划线(_)替代变量名来忽略这个值:
let base   = 3
let power  = 10
var answer = 1
for _ in 1...power { //一般用于只需获取 循环次数 而无需关心每次循环时的值的情况
    answer *= base
}
print("\(base) to the power of (power) is \(answer)") //计算 3的 10次幂

//使用 for- in 遍历数组元素
let names = ["Anna","Alex","Brain","Jack"]
for name in names {
    print("\(name)")
}
//使用 for-in 遍历字典
let numberOfLegs = ["spider": 8, "ant": 6, "cat": 4]
for (animalName,legCount) in numberOfLegs {
    print("\(animalName)s have \(legCount) legs")
}

//2.while 循环会一直运行一段语句直到条件变为 false. 该类循环适合使用在迭代次数未知的情况下.
//while 循环有两种形式:
//1> while循环,每次在循环开始时计算条件是否符合;
//2> repeat-while 循环,每次在循环结束时计算条件是否符合.

//2.1. while 循环
//while 循环从计算一个条件开始,如果条件为 true, 会重复运行一段语句,直到条件变为 false
/**
 while condition {
    statements
 }
 */

//2.2. repeat-while 循环
//repeat-while 循环与 while 的区别是在判断执行条件的时间上,repeat-while 是先执行一次循环然后判断条件,直至条件变为 false
//repeat-while 循环类似于 do-while 循环,即最少执行一次循环体
/**
 repeat {
    statements
 } while condition
 */

//3.条件语句
//swift 提供两种条件语句,即 if语句 和 switch语句,通常,当条件较为简单且可能的情况较少时使用 if语句,否则应使用 switch语句.

//3.1. if 语句
//3.2. switch 语句
//switch 语句会尝试把某个值与若干个模式进行匹配,根据第一个匹配成功的模式, switch 语句会执行对应的代码.
/**
 switch some value to consider {
    case value 1:
        respond to value 1
    case value 2,
        value 3:
        respond to value 2 or 3
    default:
        otherwise, do something else
 }
 */

//注: switch 语句必须是完备的,也就是说,每一个可能的值都必须至少有一个 case 分支与之对应,在某些不可能涵盖所有值的情况下,可以使用 default 分支来覆盖其它没有对应的值,这个默认分支必须在 switch 语句的最后面.
let someCharacter:Character = "z"
switch someCharacter {
case "a":
    print("The first letter of the alphabet")
case "z":
    print("The last letter of the alphabet")
default:
    print("Some other character")
}

//注:每一个 case 分支语句都必须要包含最少一条语句,下面的写法是不对的:
/**
 switch test {
 case 1: //这里是错误的,应包含最少一条语句
 case 2:
    print("something")
 }
 */

//单个 case 可以进行复合匹配
let anotherCharacter:Character = "a"
switch anotherCharacter {
case "a","A": //逗号分隔
    print("the letter A")
default:
    print("Not the letter A")
}

//case分支的模式也可以是一个值的区间
let approximateCount = 62
let countedThings = "moons orbiting Saturn"
var 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).")

//switch 与 元组
let somePoint = (1, 1) //(Int,Int)类型的元组
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")
}

//注:swift 允许多个 case 匹配同一个值,如果存在多个匹配,那么只会执行第一个被匹配到的 case 分支,其他的匹配项会被忽略掉.

//值绑定: case分支允许将匹配的值绑定到一个临时的常量或变量,并且在 case 分支体内使用.这种行为被称为值绑定.
let anotherPoint = (2, 0)
switch anotherPoint {
case (let x, 0): // x 为临时变量
    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))")
}
//注:上面的 switch 未包含默认分支,这是因为最后一个 case let((x,y)声明了一个可以匹配余下所有值得元组,这使得 switch 语句已经完备,因此不需要再书写默认分支.

// 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 line x == -y")
case let (x, y): //该语句的存在使得 switch 无需再写默认分支语句
    print("(\(x), \(y)) is just some arbitrary point")
}

//复合匹配:当多个条件可以使用同一种方法来处理时,可以将这几种可能放在同一个 case 后面,并且用逗号隔开,当 case 后面的任意一种模式匹配的时候,这条分支就会被匹配.并且,如果匹配列表过长,还可以分行书写.
let anotherSomeCharacter: Character = "e"
switch anotherSomeCharacter {
case "a", "e", "i", "o", "u":
    print("\(anotherSomeCharacter) is a vowel")
case "b", "c", "d", "f", "g", "h", "j", "k", "l", "m",
     "n", "p", "q", "r", "s", "t", "v", "w", "x", "y", "z":
    print("\(anotherSomeCharacter) is a consonant")
default:
    print("\(anotherSomeCharacter) is not a vowel or a consonant")
}

//符合匹配内也可以包含值绑定:复合匹配里所有的匹配模式,都必须包含相同的值绑定。并且每一个绑定都必须获取到相同类型的值。这保证了无论复合匹配中的哪个模式发生了匹配,分支体内的代码都能获取到绑定的值,并且绑定的值都有一样的类型。
let stillAnotherPoint = (9, 0)
switch stillAnotherPoint {
case (let distance, 0), (0, let distance):
    print("On an axis, \(distance) from the origin")
default:
    print("Not on an axis")
}

你可能感兴趣的:(Swift 3.0学习笔记_8_控制流)