苹果新的编程语言 Swift 语言进阶(五)--控制流
发布日期:2021-09-29 09:56:24 浏览次数:1 分类:技术文章

本文共 3488 字,大约阅读时间需要 11 分钟。

原始链接:http://blog.csdn.net/goohong/article/details/31374163

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 关键字来声明。如果想使用它作为变量,就必须对它进行声明。

    在循环语句或条件语句中声明的常量或变量仅在循环执行体或循环执行体中有效。

    如果不需要使用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,ywhere x ==y:

   println("(\(x),\(y)) is on the line x == y")

case let (x,ywhere 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

 

    }

}

                         版权所有,请转载时清楚注明链接和出处!

转载地址:https://blog.csdn.net/hengshujiyi/article/details/39375171 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:苹果新的编程语言 Swift 语言进阶(七)--枚举、结构、类
下一篇:苹果新的编程语言 Swift 语言进阶(四)--字符串和收集类型

发表评论

最新留言

不错!
[***.144.177.141]2024年03月24日 01时37分45秒