您的位置:首页 > 移动开发 > Swift

Swift学习笔记(7)--控制流

2014-06-15 01:46 405 查看
1.For循环

//1.条件递增
for var index = 0; index < 3; ++index {
println("index is \(index)")
}

//2.for in循环
//  2.1有变量名
for index in 1...5 {
println("\(index) times 5 is \(index * 5)")
}

//  2.2没有变量名,如果不需要知道区间内每一项的值,你可以使用下划线(_)替代变量名来忽略对值的访问:
let base = 3
let power = 10
var answer = 1
for _ in 1...power {
answer *= base
}

//  2.3遍历数组
let names = ["Anna", "Alex", "Brian", "Jack"]
for name in names {
println("Hello, \(name)!")
}

//  2.4遍历字典
let numberOfLegs = ["spider": 8, "ant": 6, "cat": 4]
for (animalName, legCount) in numberOfLegs {
println("\(animalName)s have \(legCount) legs")
}

//  2.5遍历字符串的字符
for character in "Hello" {
println(character)
}


  

2.While循环

//1.while循环
let maxValue = 100
var x = 0
while x < maxValue {
x++
}
println(x)  //100

//2.do while循环
var y = 0
do {
y++
} while y < maxValue
println(y)  //100


  

3.If语句

var checkValue = 10
//1.if
var x = 1
if x < checkValue {
println("x < 10")
}

//2.if else
var y = 15
if y < checkValue {
println("y < checkValue")
} else {
println("y >= checkValue")
}

//3.elseif
var z = 10
if z < checkValue {
println("z < checkValue")
} else if z > checkValue {
println("z > checkValue")
} else {
println("z = checkValue")
}


  

4.Switch语句

与 C 语言和 Objective-C 中的
switch
语句不同,在 Swift 中,当匹配的 case 分支中的代码执行完毕后,程序会终止
switch
语句,而不会继续执行下一个 case 分支。这也就是说,不需要在 case 分支中显式地使用
break
语句。这使得
switch
语句更安全、更易用,也避免了因忘记写
break
语句而产生的错误。

//1.普通匹配
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")
}// 输出 "e is a vowel"

//2.区间匹配
let count = 200
var naturalCount: String
switch count {
case 0:
naturalCount = "0"
case 1...10:
naturalCount = "a few"
case 10...100:
naturalCount = "several"
case 100...999:
naturalCount = "hundreds of"
case 1000...999_999:
naturalCount = "thousands of"
default:
naturalCount = "millions and millions of"
}
println("There are \(naturalCount).")// 输出 "There are hundreds of."

//3.(Tuple)元组匹配可以使用元组在同一个switch语句中测试多个值。元组中的元素可以是值,也可以是区间。另外,使用下划线(_)来匹配所有可能的值。
// 以2*2的坐标区间为例
let somePoint = (1, 1)
switch somePoint {
case (0, 0):
println("(0, 0)在原点")
case (_, 0):
println("(\(somePoint.0), 0)在x轴")
case (0, _):
println("(0, \(somePoint.1))在y轴")
case (-2...2, -2...2):
println("(\(somePoint.0), \(somePoint.1))在2*2区域内")
default:
println("(\(somePoint.0), \(somePoint.1))在2*2区域外")
}// 输出 "(1, 1)在2*2区域内"

//值绑定:可以将元组中的值临时绑定到一个变量中
let anotherPoint = (2, 0)
switch anotherPoint {
case (let x, 0):
println("在x轴,且x坐标值为:\(x)")
case (0, let y):
println("在y轴,且y坐标值为:\(y)")
case let (x, y):
println("坐标点:(\(x), \(y))")
}// 输出 "在x轴,且x坐标值为:2"

//case 分支的模式可以使用where语句来判断额外的条件。
let yetAnotherPoint = (1, -1)
switch yetAnotherPoint {
case let (x, y) where x == y:
println("(\(x), \(y)) 在x == y的直线上")
case let (x, y) where x == -y:
println("(\(x), \(y)) 在x == -y的直线上")
case let (x, y):
println("(\(x), \(y)) 不在对称线上")
}// 输出 "(1, -1) 在x == -y的直线上"


  

5.控制转移关键字

continue

break

fallthrough (贯穿)

return (在函数中使用,略)

//1.continue
for i in 1...10 {
if i%2 == 0 {
continue //整除2就直接进入下一次循环
}
print("\(i) ")
}   // 输出1 3 5 7 9

//2.break
for i in 1...10 {
if i%3 == 0 {
break //整除3就直接跳出for循环
}
print("\(i) ")
}   // 输出1 2
println("")

//3.fallthrough 贯穿
/*Swift 中的switch不会从上一个 case 分支落入到下一个 case 分支中。相反,只要第一个匹配到的 case 分支完成了它需要执行的语句,整个switch代码块完成了它的执行。相比之下,C 语言要求你显示的插入break语句到每个switch分支的末尾来阻止自动落入到下一个 case 分支中。Swift 的这种避免默认落入到下一个分支中的特性意味着它的switch 功能要比 C 语言的更加清晰和可预测,可以避免无意识地执行多个 case 分支从而引发的错误。

如果你确实需要 C 风格的贯穿(fallthrough)的特性,你可以在每个需要该特性的 case 分支中使用fallthrough关键字。下面的例子使用fallthrough来创建一个数字的描述语句。*/
let integerToDescribe = 5
var description = "数字 \(integerToDescribe) 是"
switch integerToDescribe {
case 2, 3, 5, 7, 11, 13, 17, 19:
description += "一个质数, 也是一个"
fallthrough
case 1, 3, 5, 7, 9, 11, 13, 15, 17, 19:
description += "奇数, 也是一个"
fallthrough
default:
description += "整数"
}
println(description)
// 输出 "数字 5 是一个质数, 也是一个奇数, 也是一个整数."


  

6.带标签控制流

在 Swift 中,你可以在循环体和switch代码块中嵌套循环体和switch代码块来创造复杂的控制流结构。然而,循环体和switch代码块两者都可以使用break语句来提前结束整个方法体。因此,显示地指明break语句想要终止的是哪个循环体或者switch代码块,会很有用。类似地,如果你有许多嵌套的循环体,显示指明continue语句想要影响哪一个循环体也会非常有用。

为了实现这个目的,你可以使用标签来标记一个循环体或者switch代码块,当使用break或者continue时,带上这个标签,可以控制该标签代表对象的中断或者执行。

let maxValue = 100
let flagValue = 55
let stepValue = 10
var i = 5

addLoop: while i < maxValue {
i = i + stepValue
switch i {
case flagValue:
println("到达标示的数字,退出wile循环")
break addLoop
case 1...10:
println("数字\(i)介于1到10之间")
case 11...20:
println("数字\(i)介于11到20之间")
case 21...30:
println("数字\(i)介于21到30之间")
case 31...40:
println("数字\(i)介于31到40之间")
case 41...50:
println("数字\(i)介于41到50之间")
case 51...60:
println("数字\(i)介于51到60之间")

default:
println("Default")
}
}


  
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: