Swift 语言支持C语言全部的控制语句。包含for 和while循环语句,if和switch条件语句,以及break和continue控制语句等。
Swift 语言除了支持以上语句,还添加了一个for-in循环语句。来更方面地遍历数组、词典、范围、字符串和其他序列等。
1、for-in循环
for index
in 1...5 {println("\(index)
times 5 is \(index *5)")}
以上for-in循环用来遍历一个闭合的的范围。
为了语句的简洁。以上语句中使用到的index能够从循环运行体中判断出是一个常量。因此该常量不须要在使用之前使用let keyword来声明。
假设想使用它作为变量。就必须对它进行声明。
在循环语句或条件语句中声明的常量或变量仅在循环运行体或循环运行体中有效。
假设不须要使用for-in范围的每个值,以上语句还能够採用例如以下形式:
let base =3
let power =10
var answer =1
for _in 1...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运行体。
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")}
因为Swift 语言要求每一个Case分支必须包括一个至少一条语句的运行体。例如以下代码因为第一个case分支
缺少运行体将报一个编译错误,该优化也从语法上避免了一个case运行另外的case的情况,语法也更清晰。
let anotherCharacter:Character
="a"switch anotherCharacter
{case "a":
case "A":
println("The letter
A")default:
println("Not the letter
A")}
Swift 语言 的switch Case
分支能够採用很多不同类型的匹配模式,包含范围、多元组。
例如以下是一个使用多元组匹配的样例。
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")}
Swift 语言同意多个case 分支符合某个条件。在某个值匹配多个case 分支的情况下, Swift 语言规定总是使用第一个最先匹配的分支。
如以上样例尽管(0, 0)点匹配全部四个case 分支。但它仅仅运行首先匹配到的分支case
(0,0)相应的运行体,其他匹配的分支将被忽略。
下面是一个使用范围进行匹配的样例。
let count =3_000
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"}
在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))")}
每个Case分支还能使用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")}
3、传输控制语句和标签语句
Swift 除了支持标准的continue、break、return传输控制语句外,还提供了一个以上提到的fallthrough传输控制语句。
Swift 也支持循环语句或switch语句的嵌套,另外Swift 还支持为一个循环语句或switch语句加入一个标签,然后能够使用传输控制语句continue、break跳转到该标签语句处运行。例如以下所看到的:
label name:while condition
{switch condition
{case value 1:
statements 1
break label name
case value 2:
statements 2
continue label name
}
}
版权全部。请转载时清楚注明链接和出处!