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

寒城攻略:Listo 教你 25 天学会 Swift 语言 - 07 Control Flow

2015-02-25 17:41 316 查看




import Foundation

//***********************************************************************************************

//1.Control Flow(控制流)

//_______________________________________________________________________________________________

//介绍

//除了 C 里面的 for 条件递增循环, Swift 还增加了 for-in 循环,用来更加简单的遍历数组,字典,范围,字符串和其他序列类型

//Swift 中的 switch 语句也更多的用在 C,switch 语句的不需要写 break,switch 语句并且包含范围匹配,元组和投到一个特定的类型

//***********************************************************************************************

//2.For Loop(for 循环)

//_______________________________________________________________________________________________

//For-In

for index in 1...5 {
//最简单的 for in 语句来实现循环

println(index)

}

//_______________________________________________________________________________________________

//当不需要使用循环中的值的时候,可以使用 _ 来替代 index

let base = 3

let power = 10

var answer = 1

for _ in 1...power{
//使用 _ 来替代 index 元素实现循环

answer *= base

}

println("\(base) to the power of \(power) is \(answer)")

//_______________________________________________________________________________________________

//使用 for in 获取数组中的元素

let names = ["Listo", "Pin", "Melody", "Colder"]

for name in names{

println("Hello, \(name)!")

}

//_______________________________________________________________________________________________

//使用 for in 获取字典中的元素

let numberOfLeg = ["Spider": 8, "Ant": 6, "Cat": 4]

for (animalName, legCount) in numberOfLeg{

println("\(animalName)s have \(legCount) legs")

}

//_______________________________________________________________________________________________

//使用 for in 获取字符串中的每个字符

for character in "Hello"{

println(character)

}

//_______________________________________________________________________________________________

//传统的 for 循环

for var index = 0; index < 3; ++index{

println("index is \(index)")

}

//***********************************************************************************************

//3.While Loop(while 循环)

//_______________________________________________________________________________________________

//while 的使用

var number = 2 //while 循环举例

while number >= 1{

println("nice")

number--

}

//_______________________________________________________________________________________________

//do while 的用法

var number1 = 2

do{

println("nice")

number1--

}while number1 >= 1

//***********************************************************************************************

//4.Conditional Statements(条件语句)

//_______________________________________________________________________________________________

//if 语句实现条件判断

var temperatureInFahrenheit = 30

if temperatureInFahrenheit <= 32{

println("It's very cold, consider wearing a scarf")

}

//_______________________________________________________________________________________________

//if else 语句实现条件控制

temperatureInFahrenheit = 40

if temperatureInFahrenheit <= 32{

println("It's very cold. consider wearing a scarf")

}

else{

println("It's not that cold.wear a t-shirt is ok")

}

//_______________________________________________________________________________________________

//if else if else 语句实现条件控制

temperatureInFahrenheit = 90

if temperatureInFahrenheit <= 32{

println("It's very cold. consider wearing a scarf")

}

else if temperatureInFahrenheit >= 86{

println("It's really warm.Don't forget to wear a sunscreen")

}

else{

println("It's not that cold.wear a t-shirt")

}

//_______________________________________________________________________________________________

//Swtch 特性:与 C 语言和 Objectve-C 中的 switch 语句不同,在 swift 中,当匹配 case 块中的代码执行完毕后,程序会终止 switch语句,而不会继续进行 case 块。也就是不需要显式的添加 break 语句,但是每个 case 必须包含至少一条语句,否则无效

//_______________________________________________________________________________________________

//简单的 switch 语句应用举例

let someCharacter: Character = "e"

switch someCharacter{

case "a", "e", "i", "o", "u":
//当 switch 语句中的 case 语句条件并列的时候,用逗号隔开即可

println("\(someCharacter) is
a consonant")

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 consontant")

default:

println("\(someCharacter) is
not a vowel or a consonant")

}

//_______________________________________________________________________________________________

//在 switch 语句中应用范围匹配

let count = 3_000_000_000_000

let countedThings = "stars in the Milky Way"

var naturalCount: String

switch count{ //switch 语句中加入范围判断

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 = "millons and millons of"

}

println("There are \(naturalCount) \(countedThings)")

//_______________________________________________________________________________________________

//在 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 the box")

}

//_______________________________________________________________________________________________

//值绑定

let anotherPoint = (2, 0)
//case 块的模式允许将匹配的值绑定到一个临时的变量或者常量上,这些常量或者变量在该 case 块里就可以被引用了,这就是值绑定

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))")

}

//_______________________________________________________________________________________________

//where 语句

let yetAnotherPoint = (1, -1) //使用 where 添加额外的判断条件

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")

}

//***********************************************************************************************

//5.Control Transfer Statements(控制转移语句)

//_______________________________________________________________________________________________

//使用 continue 语句跳过本次迭代

let puzzleInput = "great minds think alike"

var puzzleOutput = ""

for character in puzzleInput{

switch character{

case "a", "e", "i", "o", "u", "
":

continue //只要匹配到元音字母或者空格字符的时候,就调用 continue 语句使本次迭代结束,从开始下次的循环迭代

default:

puzzleOutput.append(character)

}

}

println(puzzleOutput)

//_______________________________________________________________________________________________

//使用 break 语句跳出框架

let puzzleInput1 = "great minds think alike"

var puzzleOutput1 = ""

for character1 in puzzleInput1{

switch character1{

case "a", "e", "i", "o", "u", "
":

break //当在 switch 代码快中使用 break 语句时,只要匹配到元音字母或者空格字符的时候,就调用 break 语句使跳出 switch 语句,继续进行循环进行循环;当在 for 循环中使用 break 语句时,直接跳出循环不在进行循环

default:

puzzleOutput1.append(character1)

}

}

println(puzzleOutput1)

//_______________________________________________________________________________________________

//综合使用控制转移语句

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 //使用 break 来跳出 switch

}

if let integerValue = possibleIntegerValue{

println("The integer Value of \(numberSymbol) is \(integerValue)")

}

else{

println("An integer value could not be found for \(numberSymbol)")

}

//***********************************************************************************************

//6.Fallthrough

//_______________________________________________________________________________________________

//使用 fallthrough 并列两句 case

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

default:

description += " an integer"

}

println(description) //这个例子定义 String 类型的变量 description 并且给它设置了一个初始值。函数使用 switch 逻辑来判断 integerToDescribe 变量的值。当 integerToDescribe 的值属于列表中的质数之一的时候,该函数添加一段文字在 description 后,来表明这个数字是一个质数。然后它使用 fallthrough 关键字来 "落入"
default 分支中,default 分支添加一段额外的文字在 description 后面,然后代码结束

//***********************************************************************************************

//7.Labeled Statements(标签语句)

//_______________________________________________________________________________________________

//在 Swift 中,我们可以在循环体和 switch 代码块中嵌套循环体和 swith 代码块来创造复杂的控制流结构。然而循环体和 switch 代码块两者都可以使用 break 语句来提前结束整个方法体。因此显式的指名 break 语句想要终止的是那个循环体或者 switch 代码快,这样会很有用。显式的声明 continue 语句想要影响那一个循环体也会很有用。

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

//产生一个带标签的语句是通过在该语句的关键字的同一行前面放置一个标签,并且该标签后面需要带着一个冒号。以 while 循环体举例

/*

label name: while condition{

statements

}

*/

//_______________________________________________________________________________________________

//标签语句实例代码

var score = [97, 87, 34, 100, 88, 49,
-4, 80]

First: for s in score{

switch s/10{

case 10:

continue

case 9:

println("\(s) 分为优秀")

case 8:

println("\(s) 分为良好")

case 7:

println("\(s) 分为中等")

case 0:
//题目要求碰到异常的分数终止循环而不是跳过,当不添加标签使用 break 时,我们终止的是 switch 语句,而不是 for 循环,所以我们需要标签语句来标签 for 循环然后 break
for循环 的标签

break First

default:

println("\(s) 分为不及格")

}

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