首页 > 代码库 > 【Swift】学习笔记(五)——控制语句(if,switch,for-in,for,while,do-while)
【Swift】学习笔记(五)——控制语句(if,switch,for-in,for,while,do-while)
在代码业务中,条件判断是必不可少的,控制流程的语句在每种语言中都是差不多的。swift包括了:
if,switch,for-in,for,while,do-while
if 条件语句 (if else) (if... else if ... else)
判断条件为true时执行相关代码。例如:
var a = 0
if a > 0 {
println("a > 0")
}else if a == 0{
println("a = 0")
}else{
println("a < 0")
}
switch switch语句会尝试把某个值与若干个模式(pattern)进行匹配。根据第一个匹配成功的模式,switch语句会执行对应的代码
例如:
let someCharacter: Character = "e" switch someCharacter { case "a", "e", "i", "o", "u": println("\(someCharacter) 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": println("\(someCharacter) is a consonant") default: println("\(someCharacter) is not a vowel or a consonant") }
switch有几个注意事项:
1、case语句下必须包含至少一行代码
2、case语句下可以不包含break,它不存在隐式的贯穿
3、case可以进行区间匹配
let count = 3_000_000_000_000 let countedThings = "stars in the Milky Way" var naturalCount: String switch count { case 0: naturalCount = "no" case 1...3: naturalCount = "a few" case 4...9: naturalCount = "several" case 10...99: naturalCount = "tens of" case 100...999: naturalCount = "hundreds of" case 1000...999_999: naturalCount = "thousands of" default: naturalCount = "millions and millions of" } println("There are \(naturalCount) \(countedThings).")
4、元组 你可以使用元组在同一个switch语句中测试多个值。元组中的元素可以是值,也可以是区间。另外,使用下划线(_)来匹配所有可能的值
例如:
let somePoint = (1, 1) switch somePoint { case (0, 0): println("(0, 0) is at the origin") case (_, 0): println("(\(somePoint.0), 0) is on the x-axis") case (0, _): println("(0, \(somePoint.1)) is on the y-axis") case (-2...2, -2...2): println("(\(somePoint.0), \(somePoint.1)) is inside the box") default: println("(\(somePoint.0), \(somePoint.1)) is outside of the box") }
5、值绑定 case 分支的模式允许将匹配的值绑定到一个临时的常量或变量,这些常量或变量在该 case 分支里就可以被引用了——这种行为被称为值绑定
例如:
let anotherPoint = (2, 0) switch anotherPoint { case (let x, 0): println("on the x-axis with an x value of \(x)") case (0, let y): println("on the y-axis with a y value of \(y)") case let (x, y): println("somewhere else at (\(x), \(y))") } // 输出 "on the x-axis with an x value of 2"
6、where条件判断
例如
let yetAnotherPoint = (1, -1) switch yetAnotherPoint { case let (x, y) where x == y: println("(\(x), \(y)) is on the line x == y") case let (x, y) where x == -y: println("(\(x), \(y)) is on the line x == -y") case let (x, y): println("(\(x), \(y)) is just some arbitrary point") } // 输出 "(1, -1) is on the line x == -y"
7、能够使用return,continue,break,fallthrough 来改变所有控制语句的控制流程。这叫做控制转移。
a)continue语句告诉一个循环体立刻停止本次循环迭代,重新开始下次循环迭代。就好像在说“本次循环迭代我已经执行完了”,但是并不会离开整个循环体。
b)break语句会立刻结束整个控制流的执行。当你想要更早的结束一个switch代码块或者一个循环体时,你都可以使用break语句,if for for-in while do-while 这些都能够用break跳出控制,
c)fallthrough 贯穿 这个和第二点一样。
d)return语句会立刻终止当前代码的执行,并返回。用在方法控制中比较多。
8、给while取一个名字 例如
mainLabel: while a > 0{ if a ==100 { break mainLabel } }
for 循环 for-in循环
for循环用来按照指定的次数多次执行一系列语句。Swift 提供两种for循环形式:
a)for-in用来遍历一个区间(range),序列(sequence),集合(collection),系列(progression)里面所有的元素执行一系列语句。
b)for条件递增(for-condition-increment)语句,用来重复执行一系列语句直到达成特定条件达成,一般通过在每次循环完成后增加计数器的值来实现。
遍历区间:
for index in 1...5 { println("\(index) times 5 is \(index * 5)") }
遍历数组:
let names = ["Anna", "Alex", "Brian", "Jack"] for name in names { println("Hello, \(name)!") }
遍历字典集合:
let numberOfLegs = ["spider": 8, "ant": 6, "cat": 4] for (animalName, legCount) in numberOfLegs { println("\(animalName)s have \(legCount) legs") }
还可以遍历字符:
for character in "Hello" { println(character) }
增量循环:
for var index = 0; index < 3; ++index { println("index is \(index)") }
while循环 do-while循环
while循环,每次在循环开始时计算条件是否符合;
do-while循环,每次在循环结束时计算条件是否符合。
while循环:
while condition { statements }
while a > 0{ println("a value \(a)"); }
do-while循环
do { statements } while condition
do{ println("a value \(a)") }while a > 0
哇、好强大,这循环用起来应该很爽啊。。。虽然用得最多的就是for循环,呵呵。
【Swift】学习笔记(五)——控制语句(if,switch,for-in,for,while,do-while)