@ -10,7 +10,7 @@
< meta content = "text/html; charset=utf-8" http-equiv = "Content-Type" >
< meta name = "robots" content = "index, follow" >
< meta name = "author" content = "" >
< meta name = "description" content = "Swift 兴趣交流群: 307017261" >
< meta name = "description" content = "Swift 兴趣交流群: 307017261Swift 开发者社区 " >
< meta name = "keywords" content = "gitbook,github" >
< meta name = "generator" content = "www.gitbook.io" >
@ -46,7 +46,7 @@
< div class = "book" data-level = "3.5" data-basepath = ".." data-revision = "1402792177330 " >
< div class = "book" data-level = "3.5" data-basepath = ".." data-revision = "1402808574723 " >
< div class = "book-header" >
<!-- Actions Left -->
< a href = "#" class = "btn pull-left toggle-summary" aria-label = "Toggle summary" > < i class = "fa fa-align-justify" > < / i > < / a >
@ -587,11 +587,11 @@
< div class = "page-inner" >
< section class = "normal" id = "section-gitbook_65 3" >
< section class = "normal" id = "section-gitbook_7 3" >
< blockquote >
< p > 翻译: coverxit< / p >
< p > 校对: numbbbbb, coverxit < / p >
< p > 翻译:< a href = "https://github.com/coverxit" target = "_blank" > coverxit< / a >
校对: < a href = "https://github.com/numbbbbb" target = "_blank" > numbbbbb< / a > , < a href = "https://github.com/coverxit" target = "_blank" > coverxit< / a > , < a href = "https://github.com/stanzhai" target = "_blank" > stanzhai< / a > < / p >
< / blockquote >
< h1 id = "-" > 语句< / h1 >
< hr >
@ -606,162 +606,113 @@
< p > 循环语句用于重复执行代码块;分支语句用于执行满足特定条件的代码块;控制传递语句则用于修改代码的执行顺序。在稍后的叙述中,将会详细地介绍每一种类型的控制流语句。< / p >
< p > 是否将分号(< code > ;< / code > )添加到语句的结尾处是可选的。但若要在同一行内写多条独立语句,请务必使用分号。< / p >
< blockquote >
< p > GRAMMAR OF A STATEMENT < / p >
< p > < em > statement< / em > → < a href = "https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Expressions.html#//apple_ref/swift/grammar/expression" target = "_blank" > < em > expression< / em > < / a > < strong > ;< / strong > < em > opt< / em > < / p >
< p > < em > statement< / em > → < a href = "https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/declaration" target = "_blank" > < em > declaration< / em > < / a > < strong > ;< / strong > < em > opt< / em > < / p >
< p > < em > statement< / em > → < a href = "https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/loop-statement" target = "_blank" > < em > loop-statement< / em > < / a > < strong > ;< / strong > < em > opt< / em > < / p >
< p > < em > statement< / em > → < a href = "https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/branch-statement" target = "_blank" > < em > branch-statement< / em > < / a > < strong > ;< / strong > < em > opt< / em > < / p >
< p > < em > statement< / em > → < a href = "https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/labeled-statement" target = "_blank" > < em > labeled-statement< / em > < / a > < / p >
< p > < em > statement< / em > → < a href = "https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/control-transfer-statement" target = "_blank" > < em > control-transfer-statement< / em > < / a > < strong > ;< / strong > < em > opt< / em > < / p >
< p > < em > statement< / em > → < a href = "https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/statement" target = "_blank" > < em > statment< / em > < / a > < a href = "https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/statements" target = "_blank" > < em > statements< / em > < / a > < strong > ;< / strong > < em > opt< / em > < / p >
< p > 语句语法< br > < em > 语句< / em > → < a href = "..\chapter3\04_Expressions.html#expression" > < em > 表达式< / em > < / a > < strong > ;< / strong > < em > 可选< / em > < br > < em > 语句< / em > → < a href = "..\chapter3\05_Declarations.html#declaration" > < em > 声明< / em > < / a > < strong > ;< / strong > < em > 可选< / em > < br > < em > 语句< / em > → < a href = "..\chapter3\10_Statements.html#loop_statement" > < em > 循环语句< / em > < / a > < strong > ;< / strong > < em > 可选< / em > < br > < em > 语句< / em > → < a href = "..\chapter3\10_Statements.html#branch_statement" > < em > 分支语句< / em > < / a > < strong > ;< / strong > < em > 可选< / em > < br > < em > 语句< / em > → < a href = "..\chapter3\10_Statements.html#labeled_statement" > < em > 标记语句(Labeled Statement)< / em > < / a > < br > < em > 语句< / em > → < a href = "..\chapter3\10_Statements.html#control_transfer_statement" > < em > 控制转移语句< / em > < / a > < strong > ;< / strong > < em > 可选< / em > < br > < em > 多条语句(Statements)< / em > → < a href = "..\chapter3\10_Statements.html#statement" > < em > 语句< / em > < / a > < a href = "..\chapter3\10_Statements.html#statements" > < em > 多条语句(Statements)< / em > < / a > < em > 可选< / em > < / p >
< / blockquote >
< p > < a name = "loop_statements" > < / a > < / p >
< h2 id = "-" > 循环语句< / h2 >
< p > 取决于特定的循环条件, 循环语句允许重复执行代码块。Swift 提供四种类型的循环语句:< code > for< / code > 语句、< code > for-in< / code > 语句、< code > while< / code > 语句和< code > do-while< / code > 语句。< / p >
< p > 通过< code > break< / code > 语句和< code > continue< / code > 语句可以改变循环语句的控制流。有关这两条语句,详情参见 < a href = "#break_statement" > Break 语句< / a > 和 < a href = "#continue_statement" > Continue 语句< / a > 。< / p >
< blockquote >
< p > GRAMMAR OF A LOOP STATEMENT < / p >
< p > < em > loop-statement< / em > → < a href = "https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/for-statement" target = "_blank" > < em > for-statement< / em > < / a > < / p >
< p > < em > loop-statement< / em > → < a href = "https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/for-in-statement" target = "_blank" > < em > for-in-statement< / em > < / a > < / p >
< p > < em > loop-statement< / em > → < a href = "https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/while-statement" target = "_blank" > < em > while-statement< / em > < / a > < / p >
< p > < em > loop-statement< / em > → < a href = "https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/do-while-statement" target = "_blank" > < em > do-while-statement< / em > < / a > < / p >
< p > 循环语句语法< br > < em > 循环语句< / em > → < a href = "..\chapter3\10_Statements.html#for_statement" > < em > for语句< / em > < / a > < br > < em > 循环语句< / em > → < a href = "..\chapter3\10_Statements.html#for_in_statement" > < em > for-in语句< / em > < / a > < br > < em > 循环语句< / em > → < a href = "..\chapter3\10_Statements.html#wheetatype类型ile_statement" > < em > while语句< / em > < / a > < br > < em > 循环语句< / em > → < a href = "..\chapter3\10_Statements.html#do_while_statement" > < em > do-while语句< / em > < / a > < / p >
< / blockquote >
< h3 id = "for-" > For 语句< / h3 >
< p > < code > for< / code > 语句允许在重复执行代码块的同时,递增一个计数器。< / p >
< p > < code > for< / code > 语句的形式如下:< / p >
< pre > < code class = "lang-swift" > for `initialzation`; `condition`; `increment` {
`statements`
}
< / code > < / pre >
< blockquote >
< p > for < code > initialzation< / code > ; < code > condition< / code > ; < code > increment< / code > {< br > < code > statements< / code > < br > } < / p >
< / blockquote >
< p > < em > initialzation< / em > 、< em > condition< / em > 和 < em > increment< / em > 之间的分号,以及包围循环体 < em > statements< / em > 的大括号都是不可省略的。< / p >
< p > < code > for< / code > 语句的执行流程如下:< / p >
< ol >
< li > < p > < em> initialzation< / em > 只会被执行一次,通常用于声明和初始化在接下来的循环中需要使用的变量。< / p >
< / li >
< li > < p > 计算 < em > condition< / em > 表达式:
如果为< code > true< / code > , < em > stat ements < / em > 将会被执行 ,然后转到第3 步。如果为< code > false< / code > , < em > statements< / em > 和 < em > increment< / em > 都不会被执行,< code > for< / code > 至此执行完毕。< / p >
< / li >
< li > < p > 计算 < em > increment< / em > 表达式, 然后转到第2步。< / p >
< / li >
< li > < em > initialzation< / em > 只会被执行一次,通常用于声明和初始化在接下来的循环中需要使用的变量。< / li >
< li > 计算 < em > condition< / em > 表达式:
如果为< code > true< / code > , < em > statements< / em > 将会被执行, 然后转到第3步。如果为< code > false< / code > , < em > statements< / em > 和 < em > increment< / em > 都不会被执行,< code > for< / code > 至此执行完毕。< / li >
< li > 计算 < em > incr ement< / em > 表达式 ,然后转到第2 步。< / li >
< / ol >
< p > 定义在 < em > initialzation< / em > 中的变量仅在< code > for< / code > 语句的作用域以内有效。< em > condition< / em > 表达式的值的类型必须遵循< code > LogicValue< / code > 协议。< / p >
< blockquote >
< p > GRAMMAR OF A FOR STATEMENT < / p >
< p > < em > for-statement< / em > → < strong > for< / strong > < a href = "https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/for-init" target = "_blank" > < em > for-init< / em > < / a > < em > opt< / em > < strong > ;< / strong > < a href = "https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Expressions.html#//apple_ref/swift/grammar/expression" target = "_blank" > < em > expression< / em > < / a > < em > opt< / em > < strong > ;< / strong > < a href = "https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Expressions.html#//apple_ref/swift/grammar/expression" target = "_blank" > < em > expression< / em > < / a > < em > opt< / em > < a href = "https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/code-block" target = "_blank" > < em > code-block< / em > < / a > < / p >
< p > < em > for-statement< / em > → < strong > for (< / strong > < a href = "https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/for-init" target = "_blank" > < em > for-init< / em > < / a > < em > opt< / em > < strong > ;< / strong > < a href = "https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Expressions.html#//apple_ref/swift/grammar/expression" target = "_blank" > < em > expression< / em > < / a > < em > opt< / em > < strong > ;< / strong > < a href = "https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Expressions.html#//apple_ref/swift/grammar/expression" target = "_blank" > < em > expression< / em > < / a > < em > opt< / em > < strong > )< / strong > < a href = "https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/code-block" target = "_blank" > < em > code-block< / em > < / a > < / p >
< p > < em > for-statement< / em > → < a href = "https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/variable-declaration" target = "_blank" > < em > variable-declaration< / em > < / a > | < a href = "https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Expressions.html#//apple_ref/swift/grammar/expression-list" target = "_blank" > < em > expression-list< / em > < / a > < / p >
< p > For 循环语法< br > < em > for语句< / em > → < strong > for< / strong > < a href = "..\chapter3\10_Statements.html#for_init" > < em > for初始条件< / em > < / a > < em > 可选< / em > < strong > ;< / strong > < a href = "..\chapter3\04_Expressions.html#expression" > < em > 表达式< / em > < / a > < em > 可选< / em > < strong > ;< / strong > < a href = "..\chapter3\04_Expressions.html#expression" > < em > 表达式< / em > < / a > < em > 可选< / em > < a href = "..\chapter3\05_Declarations.html#code_block" > < em > 代码块< / em > < / a > < br > < em > for语句< / em > → < strong > for< / strong > < strong > (< / strong > < a href = "..\chapter3\10_Statements.html#for_init" > < em > for初始条件< / em > < / a > < em > 可选< / em > < strong > ;< / strong > < a href = "..\chapter3\04_Expressions.html#expression" > < em > 表达式< / em > < / a > < em > 可选< / em > < strong > ;< / strong > < a href = "..\chapter3\04_Expressions.html#expression" > < em > 表达式< / em > < / a > < em > 可选< / em > < strong > )< / strong > < a href = "..\chapter3\05_Declarations.html#code_block" > < em > 代码块< / em > < / a > < br > < em > for初始条件< / em > → < a href = "..\chapter3\05_Declarations.html#variable_declaration" > < em > 变量声明< / em > < / a > | < a href = "..\chapter3\04_Expressions.html#expression_list" > < em > 表达式列表< / em > < / a > < / p >
< / blockquote >
< h3 id = "for-in-" > For-In 语句< / h3 >
< p > < code > for-in< / code > 语句允许在重复执行代码块的同时,迭代集合(或遵循< code > Sequence< / code > 协议的任意类型)中的每一项。< / p >
< p > < code > for-in< / code > 语句的形式如下:< / p >
< pre > < code class = "lang-swift" > for `item` in `collection` {
`statements`
}
< / code > < / pre >
< blockquote >
< p > for < code > item< / code > in < code > collection< / code > {< br > < code > statements< / code > < br > } < / p >
< / blockquote >
< p > < code > for-in< / code > 语句在循环开始前会调用 < em > collection< / em > 表达式的< code > generate< / code > 方法来获取一个生成器类型(这是一个遵循< code > Generator< / code > 协议的类型)的值。接下来循环开始,调用 < em > collection< / em > 表达式的< code > next< / code > 方法。如果其返回值不是< code > None< / code > ,它将会被赋给 < em > item< / em > ,然后执行 < em > statements< / em > ,执行完毕后回到循环开始处;否则,将不会赋值给 < em > item< / em > 也不会执行 < em > statements< / em > , < code > for-in< / code > 至此执行完毕。< / p >
< blockquote >
< p > GRAMMAR OF A FOR-IN STATEMENT < / p >
< p > < em > for-in-statement< / em > → < strong > for< / strong > < a href = "https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Patterns.html#//apple_ref/swift/grammar/pattern" target = "_blank" > < em > pattern< / em > < / a > < strong > in< / strong > < a href = "https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Expressions.html#//apple_ref/swift/grammar/expression" target = "_blank" > < em > expression< / em > < / a > < a href = "https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/code-block" target = "_blank" > < em > code-block< / em > < / a > < / p >
< p > For-In 循环语法< br > < em > for-in语句< / em > → < strong > for< / strong > < a href = "..\chapter3\07_Patterns.html#pattern" > < em > 模式< / em > < / a > < strong > in< / strong > < a href = "..\chapter3\04_Expressions.html#expression" > < em > 表达式< / em > < / a > < a href = "..\chapter3\05_Declarations.html#code_block" > < em > 代码块< / em > < / a > < / p >
< / blockquote >
< h3 id = "while-" > While 语句< / h3 >
< p > < code > while< / code > 语句允许重复执行代码块。< / p >
< p > < code > while< / code > 语句的形式如下:< / p >
< pre > < code class = "lang-swift" > while `condition` {
`statements`
}
< / code > < / pre >
< blockquote >
< p > while < code > condition< / code > {< br > < code > statements< / code > < br > } < / p >
< / blockquote >
< p > < code > while< / code > 语句的执行流程如下:< / p >
< ol >
< li > < p > 计算 < em > condition< / em > 表达式:
如果为真< code > true< / code > , 转到第2步。如果为< code > false< / code > , < code > while< / code > 至此执行完毕。< / p >
< / li >
< li > < p > 执行 < em > statements< / em > , 然后转到第1步。< / p >
< / li >
< li > 计算 < em > condition< / em > 表达式:
如果为真< code > true< / code > , 转到第2步。如果为< code > false< / code > , < code > while< / code > 至此执行完毕。< / li >
< li > 执行 < em > statements< / em > , 然后转到第1步。< / li >
< / ol >
< p > 由于 < em > condition< / em > 的值在 < em > statements< / em > 执行前就已计算出,因此< code > while< / code > 语句中的 < em > statements< / em > 可能会被执行若干次,也可能不会被执行。< / p >
< p > < em > condition< / em > 表达式的值的类型必须遵循< code > LogicValue< / code > 协议。同时,< em > condition< / em > 表达式也可以使用可选绑定,详情参见< a href = "../chapter2/01_The_Basics.html#optional_binding" > 可选绑定< / a > 。< / p >
< blockquote >
< p > GRAMMAR OF A WHILE STATEMENT < / p >
< p > < em > while-statement< / em > → < strong > while< / strong > < a href = "https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/while-condition" target = "_blank" > < em > while-condition< / em > < / a > < a href = "https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/code-block" target = "_blank" > < em > code-block< / em > < / a > < / p >
< p > < em > while-condition< / em > → < a href = "https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Expressions.html#//apple_ref/swift/grammar/expression" target = "_blank" > < em > expression< / em > < / a > | < a href = "https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/declaration" target = "_blank" > < em > declaration< / em > < / a > < / p >
< p > While 循环语法< br > < em > while语句< / em > → < strong > while< / strong > < a href = "..\chapter3\10_Statements.html#while_condition" > < em > while条件< / em > < / a > < a href = "..\chapter3\05_Declarations.html#code_block" > < em > 代码块< / em > < / a > < br > < em > while条件< / em > → < a href = "..\chapter3\04_Expressions.html#expression" > < em > 表达式< / em > < / a > | < a href = "..\chapter3\05_Declarations.html#declaration" > < em > 声明< / em > < / a > < / p >
< / blockquote >
< h3 id = "do-while-" > Do-While 语句< / h3 >
< p > < code > do-while< / code > 语句允许代码块被执行一次或多次。< / p >
< p > < code > do-while< / code > 语句的形式如下:< / p >
< pre > < code class = "lang-swift" > do {
`statements`
} while `condition`
< / code > < / pre >
< blockquote >
< p > do {< br > < code > statements< / code > < br > } while < code > condition< / code > < / p >
< / blockquote >
< p > < code > do-while< / code > 语句的执行流程如下:< / p >
< ol >
< li > < p > 执行 < em > statements< / em > , 然后转到第2步。< / p >
< / li >
< li > < p > 计算 < em > condition< / em > 表达式:
如果为< code > true< / code > , 转到第1步。如果为< code > false< / code > , < code > do-while< / code > 至此执行完毕。< / p >
< / li >
< li > 执行 < em > statements< / em > , 然后转到第2步。< / li >
< li > 计算 < em > condition< / em > 表达式:
如果为< code > true< / code > , 转到第1步。如果为< code > false< / code > , < code > do-while< / code > 至此执行完毕。< / li >
< / ol >
< p > 由于 < em > condition< / em > 表达式的值是在 < em > statements< / em > 执行后才计算出,因此< code > do-while< / code > 语句中的 < em > statements< / em > 至少会被执行一次。< / p >
< p > < em > condition< / em > 表达式的值的类型必须遵循< code > LogicValue< / code > 协议。同时,< em > condition< / em > 表达式也可以使用可选绑定,详情参见< a href = "../chapter2/01_The_Basics.html#optional_binding" > 可选绑定< / a > 。< / p >
< blockquote >
< p > GRAMMAR OF A DO-WHILE STATEMENT < / p >
< p > < em > do-while-statement< / em > → < strong > do< / strong > < a href = "https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/code-block" target = "_blank" > < em > code-block< / em > < / a > < strong > while< / strong > < a href = "https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/while-condition" target = "_blank" > < em > while-condition< / em > < / a > < / p >
< p > Do-While 循环语法< br > < em > do-while语句< / em > → < strong > do< / strong > < a href = "..\chapter3\05_Declarations.html#code_block" > < em > 代码块< / em > < / a > < strong > while< / strong > < a href = "..\chapter3\10_Statements.html#while_condition" > < em > while条件< / em > < / a > < / p >
< / blockquote >
< p > < a name = "branch_statements" > < / a > < / p >
< h2 id = "-" > 分支语句< / h2 >
< p > 取决于一个或者多个条件的值, 分支语句允许程序执行指定部分的代码。显然, 分支语句中条件的值将会决定如何分支以及执行哪一块代码。Swift 提供两种类型的分支语句:< code > if< / code > 语句和< code > switch< / code > 语句。< / p >
< p > < code > switch< / code > 语句中的控制流可以用< code > break< / code > 语句修改,详情请见< a href = "#break_statement" > Break 语句< / a > 。< / p >
< blockquote >
< p > GRAMMAR OF A BRANCH STATEMENT < / p >
< p > < em > branch-statement< / em > → < a href = "https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/if-statement" target = "_blank" > < em > if-statement< / em > < / a > < / p >
< p > < em > branch-statement< / em > → < a href = "https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/switch-statement" target = "_blank" > < em > switch-statement< / em > < / a > < / p >
< p > 分支语句语法< br > < em > 分支语句< / em > → < a href = "..\chapter3\10_Statements.html#if_statement" > < em > if语句< / em > < / a > < br > < em > 分支语句< / em > → < a href = "..\chapter3\10_Statements.html#switch_statement" > < em > switch语句< / em > < / a > < / p >
< / blockquote >
< h3 id = "if-" > If 语句< / h3 >
< p > 取决于一个或多个条件的值,< code > if< / code > 语句将决定执行哪一块代码。< / p >
< p > < code > if< / code > 语句有两种标准形式,在这两种形式里都必须有大括号。< / p >
< p > 第一种形式是当且仅当条件为真时执行代码,像下面这样:< / p >
< pre > < code class = "lang-swift" > if `condition` {
`statements`
}
< / code > < / pre >
< blockquote >
< p > if < code > condition< / code > {< br > < code > statements< / code > < br > } < / p >
< / blockquote >
< p > 第二种形式是在第一种形式的基础上添加 < em > else 语句< / em > ,当只有一个 else 语句时,像下面这样:< / p >
< pre > < code class = "lang-swift" > if `condition` {
`statements to execute if condition is true`
< blockquote >
< p > if < code > condition< / code > {
< code > statements to execute if condition is true< / code >
} else {
` statements to execute if condition is false`
}
< / code > < / pr e>
< code > statements to execute if condition is false< / code >
}< / p >
< / blockquot e>
< p > 同时, else 语句也可包含< code > if< / code > 语句,从而形成一条链来测试更多的条件,像下面这样:< / p >
< pre > < code class = "lang-swift" > if `condition 1` {
`statements to execute if condition 1 is true`
} else if `condition 2` {
`statements to execute if condition 2 is true`
}
else {
`statements to execute if both conditions are false`
}
< / code > < / pre >
< blockquote >
< p > if < code > condition 1< / code > {< br > < code > statements to execute if condition 1 is true< / code > < br > } else if < code > condition 2< / code > {< br > < code > statements to execute if condition 2 is true< / code > < br > }< br > else {< br > < code > statements to execute if both conditions are false< / code > < br > } < / p >
< / blockquote >
< p > < code > if< / code > 语句中条件的值的类型必须遵循< code > LogicValue< / code > 协议。同时,条件也可以使用可选绑定,详情参见< a href = "../chapter2/01_The_Basics.html#optional_binding" > 可选绑定< / a > 。< / p >
< blockquote >
< p > GRAMMAR OF AN IF STATEMENT < / p >
< p > < em > if-statement< / em > → < strong > if< / strong > < a href = "https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/if-condition" target = "_blank" > < em > if-condition< / em > < / a > < a href = "https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/code-block" target = "_blank" > < em > code-block< / em > < / a > < a href = "https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/else-clause" target = "_blank" > < em > else-clause< / em > < / a > < em > opt< / em > < / p >
< p > < em > if-condition< / em > → < a href = "https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Expressions.html#//apple_ref/swift/grammar/expression" target = "_blank" > < em > expression< / em > < / a > | < a href = "https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/declaration" target = "_blank" > < em > declaration< / em > < / a > < / p >
< p > < em > else-clause< / em > → < strong > else< / strong > < a href = "https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/code-block" target = "_blank" > < em > code-block< / em > < / a > | < strong > else< / strong > < a href = "https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/if-statement" target = "_blank" > < em > if-statement< / em > < / a > < em > opt< / em > < / p >
< p > If语句语法< br > < em > if语句< / em > → < strong > if< / strong > < a href = "..\chapter3\10_Statements.html#if_condition" > < em > if条件< / em > < / a > < a href = "..\chapter3\05_Declarations.html#code_block" > < em > 代码块< / em > < / a > < a href = "..\chapter3\10_Statements.html#else_clause" > < em > else子句(Clause)< / em > < / a > < em > 可选< / em > < br > < em > if条件< / em > → < a href = "..\chapter3\04_Expressions.html#expression" > < em > 表达式< / em > < / a > | < a href = "..\chapter3\05_Declarations.html#declaration" > < em > 声明< / em > < / a > < br > < em > else子句(Clause)< / em > → < strong > else< / strong > < a href = "..\chapter3\05_Declarations.html#code_block" > < em > 代码块< / em > < / a > | < strong > else< / strong > < a href = "..\chapter3\10_Statements.html#if_statement" > < em > if语句< / em > < / a > < / p >
< / blockquote >
< h3 id = "switch-" > Switch 语句< / h3 >
< p > 取决于< code > switch< / code > 语句的< em > 控制表达式( control expression) < / em > , < code > switch< / code > 语句将决定执行哪一块代码。< / p >
< p > < code > switch< / code > 语句的形式如下:< / p >
< pre > < code class = "lang-swift" > switch `control expression` {
case `pattern 1`:
`statements`
case `pattern 2` where `condition`:
`statements`
case `pattern 3` where `condition`,
`pattern 4` where `condition`:
`statements`
default:
`statements`
}
< / code > < / pre >
< blockquote >
< p > switch < code > control expression< / code > {< br > case < code > pattern 1< / code > :< br > < code > statements< / code > < br > case < code > pattern 2< / code > where < code > condition< / code > :< br > < code > statements< / code > < br > case < code > pattern 3< / code > where < code > condition< / code > ,< br > < code > pattern 4< / code > where < code > condition< / code > :< br > < code > statements< / code > < br > default:< br > < code > statements< / code > < br > } < / p >
< / blockquote >
< p > < code > switch< / code > 语句的< em > 控制表达式( control expression) < / em > 会首先被计算,然后与每一个 case 的模式( pattern) 进行匹配。如果匹配成功, 程序将会执行对应的 case 分支里的 < em > statements< / em > 。另外,每一个 case 分支都不能为空,也就是说在每一个 case 分支中至少有一条语句。如果你不想在匹配到的 case 分支中执行代码,只需在该分支里写一条< code > break< / code > 语句即可。< / p >
< p > 可以用作控制表达式的值是十分灵活的,除了标量类型(scalar types, 如< code > Int< / code > 、< code > Character< / code > )外, 你可以使用任何类型的值, 包括浮点数、字符串、元组、自定义类的实例和可选( optional) 类型, 甚至是枚举类型中的成员值和指定的范围(range)等。关于在< code > switch< / code > 语句中使用这些类型,详情参见< a href = "../chapter2/05_Control_Flow.html" > 控制流< / a > 一章的 < a href = "../chapter2/05_Control_Flow.html#switch" > Switch< / a > 。< / p >
< p > 你可以在模式后面添加一个起保护作用的表达式(guard expression)。< em > 起保护作用的表达式< / em > 是这样构成的:关键字< code > where< / code > 后面跟着一个作为额外测试条件的表达式。因此,当且仅当< em > 控制表达式< / em > 匹配一个< em > case< / em > 的某个模式且起保护作用的表达式为真时,对应 case 分支中的 < em > statements< / em > 才会被执行。在下面的例子中,< em > 控制表达式< / em > 只会匹配含两个相等元素的元组,如< code > (1, 1)< / code > : < / p >
@ -775,16 +726,7 @@ else {
< h4 id = "-fall-through-" > 不存在隐式的贯穿(fall through)< / h4 >
< p > 当匹配的 case 分支中的代码执行完毕后,程序会终止< code > switch< / code > 语句,而不会继续执行下一个 case 分支。这就意味着,如果你想执行下一个 case 分支,需要显式地在你需要的 case 分支里使用< code > fallthrough< / code > 语句。关于< code > fallthrough< / code > 语句的更多信息,详情参见 < a href = "#fallthrough_statement" > Fallthrough 语句< / a > 。< / p >
< blockquote >
< p > GRAMMAR OF A SWITCH STATEMENT < / p >
< p > < em > switch-statement< / em > → < strong > switch< / strong > < a href = "https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Expressions.html#//apple_ref/swift/grammar/expression" target = "_blank" > < em > expression< / em > < / a > < strong > {< / strong > < a href = "https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/switch-cases" target = "_blank" > < em > switch-cases< / em > < / a > < em > opt< / em > < strong > }< / strong > < / p >
< p > < em > switch-cases< / em > → < a href = "https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/switch-case" target = "_blank" > < em > switch-case< / em > < / a > < a href = "https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/switch-cases" target = "_blank" > < em > switch-cases< / em > < / a > < em > opt< / em > < / p >
< p > < em > switch-case< / em > → < a href = "https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/case-label" target = "_blank" > < em > case-label< / em > < / a > < a href = "https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/statements" target = "_blank" > < em > statement< / em > < / a > | < a href = "https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/default-label" target = "_blank" > < em > default-label< / em > < / a > < a href = "https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/statements" target = "_blank" > < em > statements< / em > < / a > < / p >
< p > < em > switch-case< / em > → < a href = "https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/case-label" target = "_blank" > < em > case-label< / em > < / a > < strong > ;< / strong > | < a href = "https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/default-label" target = "_blank" > < em > default-label< / em > < / a > < strong > ;< / strong > < / p >
< p > < em > case-label< / em > → < strong > case< / strong > < a href = "https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/case-item-list" target = "_blank" > < em > case-item-list< / em > < / a > < strong > :< / strong > < / p >
< p > < em > case-item-list< / em > → < a href = "https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Patterns.html#//apple_ref/swift/grammar/pattern" target = "_blank" > < em > pattern< / em > < / a > < a href = "https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/guard-clause" target = "_blank" > < em > guard-clause< / em > < / a > < em > opt< / em > | < a href = "https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Patterns.html#//apple_ref/swift/grammar/pattern" target = "_blank" > < em > pattern< / em > < / a > < a href = "https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/guard-clause" target = "_blank" > < em > guard-clause< / em > < / a > < em > opt< / em > , < a href = "https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/case-item-list" target = "_blank" > < em > case-item-list< / em > < / a > < / p >
< p > < em > default-label< / em > → < strong > default :< / strong > < / p >
< p > < em > guard-clause< / em > → < strong > where< / strong > < a href = "https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/guard-expression" target = "_blank" > < em > guard-expression< / em > < / a > < / p >
< p > < em > guard-expression< / em > → < a href = "https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Expressions.html#//apple_ref/swift/grammar/expression" target = "_blank" > < em > expression< / em > < / a > < / p >
< p > Switch语句语法< br > < em > switch语句< / em > → < strong > switch< / strong > < a href = "..\chapter3\04_Expressions.html#expression" > < em > 表达式< / em > < / a > < strong > {< / strong > < a href = "..\chapter3\10_Statements.html#switch_cases" > < em > SwitchCase列表< / em > < / a > < em > 可选< / em > < strong > }< / strong > < br > < em > SwitchCase列表< / em > → < a href = "..\chapter3\10_Statements.html#switch_case" > < em > SwitchCase< / em > < / a > < a href = "..\chapter3\10_Statements.html#switch_cases" > < em > SwitchCase列表< / em > < / a > < em > 可选< / em > < br > < em > SwitchCase< / em > → < a href = "..\chapter3\10_Statements.html#case_label" > < em > case标签< / em > < / a > < a href = "..\chapter3\10_Statements.html#statements" > < em > 多条语句(Statements)< / em > < / a > | < a href = "..\chapter3\10_Statements.html#default_label" > < em > default标签< / em > < / a > < a href = "..\chapter3\10_Statements.html#statements" > < em > 多条语句(Statements)< / em > < / a > < br > < em > SwitchCase< / em > → < a href = "..\chapter3\10_Statements.html#case_label" > < em > case标签< / em > < / a > < strong > ;< / strong > | < a href = "..\chapter3\10_Statements.html#default_label" > < em > default标签< / em > < / a > < strong > ;< / strong > < br > < em > case标签< / em > → < strong > case< / strong > < a href = "..\chapter3\10_Statements.html#case_item_list" > < em > case项列表< / em > < / a > < strong > :< / strong > < br > < em > case项列表< / em > → < a href = "..\chapter3\07_Patterns.html#pattern" > < em > 模式< / em > < / a > < a href = "..\chapter3\10_Statements.html#guard_clause" > < em > guard-clause< / em > < / a > < em > 可选< / em > | < a href = "..\chapter3\07_Patterns.html#pattern" > < em > 模式< / em > < / a > < a href = "..\chapter3\10_Statements.html#guard_clause" > < em > guard-clause< / em > < / a > < em > 可选< / em > < strong > ,< / strong > < a href = "..\chapter3\10_Statements.html#case_item_list" > < em > case项列表< / em > < / a > < br > < em > default标签< / em > → < strong > default< / strong > < strong > :< / strong > < br > < em > guard-clause< / em > → < strong > where< / strong > < a href = "..\chapter3\10_Statements.html#guard_expression" > < em > guard-expression< / em > < / a > < br > < em > guard-expression< / em > → < a href = "..\chapter3\04_Expressions.html#expression" > < em > 表达式< / em > < / a > < / p >
< / blockquote >
< p > < a name = "labeled_statement" > < / a >
< a name = "control_transfer_statements" > < / a > 带标签的语句< / p >
@ -792,48 +734,39 @@ else {
< p > 标签的作用域是该标签所标记的语句之后的所有语句。你可以不使用带标签的语句,但只要使用它,标签名就必唯一。< / p >
< p > 关于使用带标签的语句的例子,详情参见< a href = "../chapter2/05_Control_Flow.html" > 控制流< / a > 一章的< a href = "../chapter2/05_Control_Flow.html#labeled_statements" > 带标签的语句< / a > 。< / p >
< blockquote >
< p > GRAMMAR OF A LABELED STATEMENT < / p >
< p > < em > labeled-statement< / em > → < a href = "https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/statement-label" target = "_blank" > < em > statement-label< / em > < / a > < a href = "https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/loop-statement" target = "_blank" > < em > loop-statement< / em > < / a > | < a href = "https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/statement-label" target = "_blank" > < em > statement-label< / em > < / a > < a href = "https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/switch-statement" target = "_blank" > < em > switch-statement< / em > < / a > < / p >
< p > < em > statement-label< / em > → < a href = "https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/label-name" target = "_blank" > < em > label-name< / em > < / a > < strong > :< / strong > < / p >
< p > < em > label-name< / em > → < a href = "https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/LexicalStructure.html#//apple_ref/swift/grammar/identifier" target = "_blank" > < em > identifier< / em > < / a > < / p >
< p > 标记语句语法< br > < em > 标记语句(Labeled Statement)< / em > → < a href = "..\chapter3\10_Statements.html#statement_label" > < em > 语句标签< / em > < / a > < a href = "..\chapter3\10_Statements.html#loop_statement" > < em > 循环语句< / em > < / a > | < a href = "..\chapter3\10_Statements.html#statement_label" > < em > 语句标签< / em > < / a > < a href = "..\chapter3\10_Statements.html#switch_statement" > < em > switch语句< / em > < / a > < br > < em > 语句标签< / em > → < a href = "..\chapter3\10_Statements.html#label_name" > < em > 标签名称< / em > < / a > < strong > :< / strong > < br > < em > 标签名称< / em > → < a href = "..\chapter3\02_Lexical_Structure.html#identifier" > < em > 标识符< / em > < / a > < / p >
< / blockquote >
< h2 id = "-" > 控制传递语句< / h2 >
< p > 通过无条件地把控制权从一片代码传递到另一片代码, 控制传递语句能够改变代码执行的顺序。Swift 提供四种类型的控制传递语句:< code > break< / code > 语句、< code > continue< / code > 语句、< code > fallthrough< / code > 语句和< code > return< / code > 语句。< / p >
< blockquote >
< p > GRAMMAR OF A CONTROL TRANSER STATEMENT < / p >
< p > < em > control-transfer-statement< / em > → < a href = "https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/break-statement" target = "_blank" > < em > break-statement< / em > < / a > < / p >
< p > < em > control-transfer-statement< / em > → < a href = "https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/continue-statement" target = "_blank" > < em > continue-statement< / em > < / a > < / p >
< p > < em > control-transfer-statement< / em > → < a href = "https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/fallthrough-statement" target = "_blank" > < em > fallthrough-statement< / em > < / a > < / p >
< p > < em > control-transfer-statement< / em > → < a href = "https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/return-statement" target = "_blank" > < em > return-statement< / em > < / a > < / p >
< p > 控制传递语句(Control Transfer Statement) 语法< br > < em > 控制传递语句< / em > → < a href = "..\chapter3\10_Statements.html#break_statement" > < em > break语句< / em > < / a > < br > < em > 控制传递语句< / em > → < a href = "..\chapter3\10_Statements.html#continue_statement" > < em > continue语句< / em > < / a > < br > < em > 控制传递语句< / em > → < a href = "..\chapter3\10_Statements.html#fallthrough_statement" > < em > fallthrough语句< / em > < / a > < br > < em > 控制传递语句< / em > → < a href = "..\chapter3\10_Statements.html#return_statement" > < em > return语句< / em > < / a > < / p >
< / blockquote >
< p > < a name = "break_statement" > < / a > < / p >
< h3 id = "break-" > Break 语句< / h3 >
< p > < code > break< / code > 语句用于终止循环或< code > switch< / code > 语句的执行。使用< code > break< / code > 语句时,可以只写< code > break< / code > 这个关键词,也可以在< code > break< / code > 后面跟上标签名( label name) , 像下面这样: < / p >
< pre > < code class = "lang-swift" > break
break `label name`
< / code > < / pr e>
< blockquote >
< p > break< br > break < code > label name< / code > < / p >
< / blockquot e>
< p > 当< code > break< / code > 语句后面带标签名时,可用于终止由这个标签标记的循环或< code > switch< / code > 语句的执行。< / p >
< p > 而当只写< code > break< / code > 时,则会终止< code > switch< / code > 语句或上下文中包含< code > break< / code > 语句的最内层循环的执行。< / p >
< p > 在这两种情况下,控制权都会被传递给循环或< code > switch< / code > 语句外面的第一行语句。< / p >
< p > 关于使用< code > break< / code > 语句的例子,详情参见< a href = "../chapter2/05_Control_Flow.html" > 控制流< / a > 一章的 < a href = "../chapter2/05_Control_Flow.html#break" > Break< / a > 和< a href = "../chapter2/05_Control_Flow.html#labeled_statements" > 带标签的语句< / a > 。< / p >
< blockquote >
< p > GRAMMAR OF A BREAK STATEMENT < / p >
< p > < em > break-statement< / em > → < strong > break< / strong > < a href = "https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/label-name" target = "_blank" > < em > label-name< / em > < / a > < em > opt< / em > < / p >
< p > Break 语句语法< br > < em > break语句< / em > → < strong > break< / strong > < a href = "..\chapter3\10_Statements.html#label_name" > < em > 标签名称< / em > < / a > < em > 可选< / em > < / p >
< / blockquote >
< p > < a name = "continue_statement" > < / a > < / p >
< h3 id = "continue-" > Continue 语句< / h3 >
< p > < code > continue< / code > 语句用于终止循环中当前迭代的执行,但不会终止该循环的执行。使用< code > continue< / code > 语句时,可以只写< code > continue< / code > 这个关键词,也可以在< code > continue< / code > 后面跟上标签名( label name) , 像下面这样: < / p >
< pre > < code class = "lang-swift" > continue
continue `label name`
< / code > < / pr e>
< blockquote >
< p > continue< br > continue < code > label name< / code > < / p >
< / blockquot e>
< p > 当< code > continue< / code > 语句后面带标签名时,可用于终止由这个标签标记的循环中当前迭代的执行。< / p >
< p > 而当只写< code > break< / code > 时,可用于终止上下文中包含< code > continue< / code > 语句的最内层循环中当前迭代的执行。< / p >
< p > 在这两种情况下,控制权都会被传递给循环外面的第一行语句。< / p >
< p > 在< code > for< / code > 语句中,< code > continue< / code > 语句执行后,< em > increment< / em > 表达式还是会被计算,这是因为每次循环体执行完毕后 < em > increment< / em > 表达式都会被计算。< / p >
< p > 关于使用< code > continue< / code > 语句的例子,详情参见< a href = "../chapter2/05_Control_Flow.html" > 控制流< / a > 一章的 < a href = "../chapter2/05_Control_Flow.html#continue" > Continue< / a > 和< a href = "../chapter2/05_Control_Flow.html#labeled_statements" > 带标签的语句< / a > 。< / p >
< blockquote >
< p > GRAMMAR OF A CONTINUE STATEMENT < / p >
< p > < em > continue-statement< / em > → < strong > continue< / strong > < a href = "https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/label-name" target = "_blank" > < em > label-name< / em > < / a > < em > opt< / em > < / p >
< p > Continue 语句语法< br > < em > continue语句< / em > → < strong > continue< / strong > < a href = "..\chapter3\10_Statements.html#label_name" > < em > 标签名称< / em > < / a > < em > 可选< / em > < / p >
< / blockquote >
< p > < a name = "fallthrough_statement" > < / a > < / p >
< h3 id = "fallthrough-" > Fallthrough 语句< / h3 >
@ -841,20 +774,18 @@ continue `label name`
< p > < code > fallthrough< / code > 语句可出现在< code > switch< / code > 语句中的任意 case 里,但不能出现在最后一个 case 分支中。同时,< code > fallthrough< / code > 语句也不能把控制权传递给使用了可选绑定的 case 分支。< / p >
< p > 关于在< code > switch< / code > 语句中使用< code > fallthrough< / code > 语句的例子,详情参见< a href = "../chapter2/05_Control_Flow.html" > 控制流< / a > 一章的< a href = "../chapter2/05_Control_Flow.html#control_transfer_statements" > 控制传递语句< / a > 。< / p >
< blockquote >
< p > GRAMMAR OF A FALLTHROUGH STATEMENT < / p >
< p > < em > continue-statement< / em > → < strong > fallthrough< / strong > < / p >
< p > Fallthrough 语句语法< br > < em > fallthrough语句< / em > → < strong > fallthrough< / strong > < / p >
< / blockquote >
< h3 id = "return-" > Return 语句< / h3 >
< p > < code > return< / code > 语句用于在函数或方法的实现中将控制权传递给调用者,接着程序将会从调用者的位置继续向下执行。< / p >
< p > 使用< code > return< / code > 语句时,可以只写< code > return< / code > 这个关键词,也可以在< code > return< / code > 后面跟上表达式,像下面这样:< / p >
< pre > < code class = "lang-swift" > return
return `expression`
< / code > < / pr e>
< blockquote >
< p > return< br > return < code > expression< / code > < / p >
< / blockquot e>
< p > 当< code > return< / code > 语句后面带表达式时, 表达式的值将会返回给调用者。如果表达式值的类型与调用者期望的类型不匹配, Swift 则会在返回表达式的值之前将表达式值的类型转换为调用者期望的类型。< / p >
< p > 而当只写< code > return< / code > 时,仅仅是将控制权从该函数或方法传递给调用者,而不返回一个值。(这就是说,该函数或方法的返回类型为< code > Void< / code > 或< code > ()< / code > ) < / p >
< blockquote >
< p > GRAMMAR OF A RETURN STATEMENT < / p >
< p > < em > return-statement< / em > → < strong > return< / strong > < a href = "https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Expressions.html#//apple_ref/swift/grammar/expression" target = "_blank" > < em > expression< / em > < / a > < em > opt< / em > < / p >
< p > Return 语句语法< br > < em > return语句< / em > → < strong > return< / strong > < a href = "..\chapter3\04_Expressions.html#expression" > < em > 表达式< / em > < / a > < em > 可选< / em > < / p >
< / blockquote >