苹果新的编程语言 Swift 语言进阶(五)--控制流
Swift 语言支持C语言全部的控制语句。包含for 和while循环语句,if和switch条件语句,以及break和continue控制语句等。 Swift 语言除了支持以上语句,还添加了一个for-in循环语句。来更方面地遍历数组、词典、范围、字符串和其他序列等。 1、for-in循环 forindexin1...5{ println("\(index)times 5 is\(index*5)") } 以上for-in循环用来遍历一个闭合的的范围。 为了语句的简洁。以上语句中使用到的index能够从循环运行体中判断出是一个常量。因此该常量不须要在使用之前使用let keyword来声明。 假设想使用它作为变量。就必须对它进行声明。 在循环语句或条件语句中声明的常量或变量仅在循环运行体或循环运行体中有效。 假设不须要使用for-in范围的每个值,以上语句还能够採用例如以下形式: letbase=3 letpower=10 varanswer=1 for_in1...power{ answer*=base } 2 Switch语句 Swift 语言对switch语法进行了优化,功能做了增强。 优化后的switch 语法更加安全、语义更加清楚。 Swift 语言要求switch的每一个分支必须是全面的。即switch声明的每一个可能的值必须运行Case分支之中的一个。假设每一个Case分支已经全面覆盖了每种情况,default 分支语句就能够省去。 Swift 语言不同意带有空运行体的Case分支, 每一个Case运行体必须相应一个Case分支,但多个可能的值能够放到一个Case声明中,用于相应一个运行分支,一个Case分支的多个匹配值由逗号切割,相应一个Case分支的多个匹配值能够分成多个行显示。 Swift 语言不须要在每一个Case分支加入一个多余的break语句。Swift 语言运行完一个Case分支的运行体后,自己主动退出switch语句,这样能够避免C语言常常出现的因为缺少break语句引起的逻辑错误。 Swift 语言支持使用Fallthrough语句来明白说明在一个Case运行体运行完后不退出switch语句而是直接运行接着的Case运行体或者default运行体。 letsomeCharacter:Character="e" switchsomeCharacter{ 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") } 因为Swift 语言要求每一个Case分支必须包括一个至少一条语句的运行体。例如以下代码因为第一个case分支 缺少运行体将报一个编译错误,该优化也从语法上避免了一个case运行另外的case的情况,语法也更清晰。 letanotherCharacter:Character="a" switchanotherCharacter{ case"a": case"A": println("The letter A") default: println("Not the letter A") } Swift 语言 的switchCase 分支能够採用很多不同类型的匹配模式,包含范围、多元组。 例如以下是一个使用多元组匹配的样例。 letsomePoint= (1,1) switchsomePoint{ 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") } Swift 语言同意多个case 分支符合某个条件。在某个值匹配多个case 分支的情况下, Swift 语言规定总是使用第一个最先匹配的分支。 如以上样例尽管(0, 0)点匹配全部四个case 分支。但它仅仅运行首先匹配到的分支case (0,0)相应的运行体,其他匹配的分支将被忽略。 下面是一个使用范围进行匹配的样例。 letcount=3_000 varnaturalCount:String switchcount{ case0: naturalCount="no" case1...3: naturalCount="a few" case4...9: naturalCount="several" case10...99: naturalCount="tens of" case100...999: naturalCount="hundreds of" case1000...999_999: naturalCount="thousands of" default: naturalCount="millions and millions of" } 在Case分支中。匹配值还能被绑定到一个暂时常量或变量,以便在也仅仅能在Case的运行体中使用。 例如以下是一个使用值绑定的样例。 letanotherPoint= (2,0) switchanotherPoint{ case(letx,0): println("on the x-axis with an x value of\(x)") case(0,lety): println("on the y-axis with a y value of\(y)") caselet(x,y): println("somewhere else at (\(x),\(y))") } 每个Case分支还能使用where从句来检查额外的更加复杂的条件。例如以下所看到的: letyetAnotherPoint= (1, -1) switchyetAnotherPoint{ caselet(x,y)wherex==y: println("(\(x),\(y)) is on the line x == y") caselet(x,y)wherex== -y: println("(\(x),\(y)) is on the line x == -y") caselet(x,y): println("(\(x),\(y)) is just some arbitrary point") } 3、传输控制语句和标签语句 Swift 除了支持标准的continue、break、return传输控制语句外,还提供了一个以上提到的fallthrough传输控制语句。 Swift 也支持循环语句或switch语句的嵌套,另外Swift 还支持为一个循环语句或switch语句加入一个标签,然后能够使用传输控制语句continue、break跳转到该标签语句处运行。例如以下所看到的: label name:whilecondition{ switchcondition{ casevalue 1: statements 1 breaklabel name casevalue 2: statements 2 continuelabel name } } 版权全部。请转载时清楚注明链接和出处! 本文转自mfrbuaa博客园博客,原文链接:http://www.cnblogs.com/mfrbuaa/p/5132612.html,如需转载请自行联系原作者