fix 3.10 statements
This commit is contained in:
@ -95,10 +95,10 @@ _________________
|
||||
<p></p>
|
||||
|
||||
> 控制传递语句(Control Transfer Statement) 语法
|
||||
> *控制转移语句* → [*break语句*](..\chapter3\10_Statements.html#break_statement)
|
||||
> *控制转移语句* → [*continue语句*](..\chapter3\10_Statements.html#continue_statement)
|
||||
> *控制转移语句* → [*fallthrough语句*](..\chapter3\10_Statements.html#fallthrough_statement)
|
||||
> *控制转移语句* → [*return语句*](..\chapter3\10_Statements.html#return_statement)
|
||||
> *控制传递语句* → [*break语句*](..\chapter3\10_Statements.html#break_statement)
|
||||
> *控制传递语句* → [*continue语句*](..\chapter3\10_Statements.html#continue_statement)
|
||||
> *控制传递语句* → [*fallthrough语句*](..\chapter3\10_Statements.html#fallthrough_statement)
|
||||
> *控制传递语句* → [*return语句*](..\chapter3\10_Statements.html#return_statement)
|
||||
|
||||
<p></p>
|
||||
|
||||
|
||||
@ -1,6 +1,5 @@
|
||||
> 翻译:coverxit
|
||||
|
||||
> 校对:numbbbbb, coverxit
|
||||
> 翻译:coverxit
|
||||
> 校对:numbbbbb, coverxit, stanzhai
|
||||
|
||||
# 语句
|
||||
-----------------
|
||||
@ -18,21 +17,14 @@
|
||||
|
||||
是否将分号(`;`)添加到语句的结尾处是可选的。但若要在同一行内写多条独立语句,请务必使用分号。
|
||||
|
||||
> GRAMMAR OF A STATEMENT
|
||||
|
||||
> *statement* → [*expression*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Expressions.html#//apple_ref/swift/grammar/expression)**;** *opt*
|
||||
|
||||
> *statement* → [*declaration*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/declaration)**;** *opt*
|
||||
|
||||
> *statement* → [*loop-statement*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/loop-statement)**;** *opt*
|
||||
|
||||
> *statement* → [*branch-statement*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/branch-statement)**;** *opt*
|
||||
|
||||
> *statement* → [*labeled-statement*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/labeled-statement)
|
||||
|
||||
> *statement* → [*control-transfer-statement*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/control-transfer-statement)**;** *opt*
|
||||
|
||||
> *statement* → [*statment*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/statement) [*statements*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/statements)**;** *opt*
|
||||
> 语句语法
|
||||
> *语句* → [*表达式*](..\chapter3\04_Expressions.html#expression) **;** _可选_
|
||||
> *语句* → [*声明*](..\chapter3\05_Declarations.html#declaration) **;** _可选_
|
||||
> *语句* → [*循环语句*](..\chapter3\10_Statements.html#loop_statement) **;** _可选_
|
||||
> *语句* → [*分支语句*](..\chapter3\10_Statements.html#branch_statement) **;** _可选_
|
||||
> *语句* → [*标记语句(Labeled Statement)*](..\chapter3\10_Statements.html#labeled_statement)
|
||||
> *语句* → [*控制转移语句*](..\chapter3\10_Statements.html#control_transfer_statement) **;** _可选_
|
||||
> *多条语句(Statements)* → [*语句*](..\chapter3\10_Statements.html#statement) [*多条语句(Statements)*](..\chapter3\10_Statements.html#statements) _可选_
|
||||
|
||||
<a name="loop_statements"></a>
|
||||
## 循环语句
|
||||
@ -41,15 +33,11 @@
|
||||
|
||||
通过`break`语句和`continue`语句可以改变循环语句的控制流。有关这两条语句,详情参见 [Break 语句](#break_statement)和 [Continue 语句](#continue_statement)。
|
||||
|
||||
> GRAMMAR OF A LOOP STATEMENT
|
||||
|
||||
> *loop-statement* → [*for-statement*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/for-statement)
|
||||
|
||||
> *loop-statement* → [*for-in-statement*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/for-in-statement)
|
||||
|
||||
> *loop-statement* → [*while-statement*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/while-statement)
|
||||
|
||||
> *loop-statement* → [*do-while-statement*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/do-while-statement)
|
||||
> 循环语句语法
|
||||
> *循环语句* → [*for语句*](..\chapter3\10_Statements.html#for_statement)
|
||||
> *循环语句* → [*for-in语句*](..\chapter3\10_Statements.html#for_in_statement)
|
||||
> *循环语句* → [*while语句*](..\chapter3\10_Statements.html#wheetatype类型ile_statement)
|
||||
> *循环语句* → [*do-while语句*](..\chapter3\10_Statements.html#do_while_statement)
|
||||
|
||||
### For 语句
|
||||
|
||||
@ -57,32 +45,25 @@
|
||||
|
||||
`for`语句的形式如下:
|
||||
|
||||
```swift
|
||||
for `initialzation`; `condition`; `increment` {
|
||||
`statements`
|
||||
}
|
||||
```
|
||||
> for `initialzation`; `condition`; `increment` {
|
||||
> `statements`
|
||||
> }
|
||||
|
||||
*initialzation*、*condition* 和 *increment* 之间的分号,以及包围循环体 *statements* 的大括号都是不可省略的。
|
||||
|
||||
`for`语句的执行流程如下:
|
||||
|
||||
1. *initialzation* 只会被执行一次,通常用于声明和初始化在接下来的循环中需要使用的变量。
|
||||
|
||||
2. 计算 *condition* 表达式:
|
||||
如果为`true`,*statements* 将会被执行,然后转到第3步。如果为`false`,*statements* 和 *increment* 都不会被执行,`for`至此执行完毕。
|
||||
|
||||
3. 计算 *increment* 表达式,然后转到第2步。
|
||||
|
||||
定义在 *initialzation* 中的变量仅在`for`语句的作用域以内有效。*condition* 表达式的值的类型必须遵循`LogicValue`协议。
|
||||
|
||||
> GRAMMAR OF A FOR STATEMENT
|
||||
|
||||
> *for-statement* → **for** [*for-init*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/for-init) *opt* **;** [*expression*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Expressions.html#//apple_ref/swift/grammar/expression) *opt* **;** [*expression*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Expressions.html#//apple_ref/swift/grammar/expression) *opt* [*code-block*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/code-block)
|
||||
|
||||
> *for-statement* → **for (** [*for-init*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/for-init) *opt* **;** [*expression*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Expressions.html#//apple_ref/swift/grammar/expression) *opt* **;** [*expression*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Expressions.html#//apple_ref/swift/grammar/expression) *opt* **)** [*code-block*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/code-block)
|
||||
|
||||
> *for-statement* → [*variable-declaration*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/variable-declaration) | [*expression-list*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Expressions.html#//apple_ref/swift/grammar/expression-list)
|
||||
> For 循环语法
|
||||
> *for语句* → **for** [*for初始条件*](..\chapter3\10_Statements.html#for_init) _可选_ **;** [*表达式*](..\chapter3\04_Expressions.html#expression) _可选_ **;** [*表达式*](..\chapter3\04_Expressions.html#expression) _可选_ [*代码块*](..\chapter3\05_Declarations.html#code_block)
|
||||
> *for语句* → **for** **(** [*for初始条件*](..\chapter3\10_Statements.html#for_init) _可选_ **;** [*表达式*](..\chapter3\04_Expressions.html#expression) _可选_ **;** [*表达式*](..\chapter3\04_Expressions.html#expression) _可选_ **)** [*代码块*](..\chapter3\05_Declarations.html#code_block)
|
||||
> *for初始条件* → [*变量声明*](..\chapter3\05_Declarations.html#variable_declaration) | [*表达式列表*](..\chapter3\04_Expressions.html#expression_list)
|
||||
|
||||
### For-In 语句
|
||||
|
||||
@ -90,17 +71,14 @@ for `initialzation`; `condition`; `increment` {
|
||||
|
||||
`for-in`语句的形式如下:
|
||||
|
||||
```swift
|
||||
for `item` in `collection` {
|
||||
`statements`
|
||||
}
|
||||
```
|
||||
> for `item` in `collection` {
|
||||
> `statements`
|
||||
> }
|
||||
|
||||
`for-in`语句在循环开始前会调用 *collection* 表达式的`generate`方法来获取一个生成器类型(这是一个遵循`Generator`协议的类型)的值。接下来循环开始,调用 *collection* 表达式的`next`方法。如果其返回值不是`None`,它将会被赋给 *item*,然后执行 *statements*,执行完毕后回到循环开始处;否则,将不会赋值给 *item* 也不会执行 *statements*,`for-in`至此执行完毕。
|
||||
|
||||
> GRAMMAR OF A FOR-IN STATEMENT
|
||||
|
||||
> *for-in-statement* → **for** [*pattern*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Patterns.html#//apple_ref/swift/grammar/pattern) **in** [*expression*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Expressions.html#//apple_ref/swift/grammar/expression) [*code-block*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/code-block)
|
||||
> For-In 循环语法
|
||||
> *for-in语句* → **for** [*模式*](..\chapter3\07_Patterns.html#pattern) **in** [*表达式*](..\chapter3\04_Expressions.html#expression) [*代码块*](..\chapter3\05_Declarations.html#code_block)
|
||||
|
||||
### While 语句
|
||||
|
||||
@ -108,28 +86,23 @@ for `item` in `collection` {
|
||||
|
||||
`while`语句的形式如下:
|
||||
|
||||
```swift
|
||||
while `condition` {
|
||||
`statements`
|
||||
}
|
||||
```
|
||||
> while `condition` {
|
||||
> `statements`
|
||||
> }
|
||||
|
||||
`while`语句的执行流程如下:
|
||||
|
||||
1. 计算 *condition* 表达式:
|
||||
如果为真`true`,转到第2步。如果为`false`,`while`至此执行完毕。
|
||||
|
||||
2. 执行 *statements* ,然后转到第1步。
|
||||
|
||||
由于 *condition* 的值在 *statements* 执行前就已计算出,因此`while`语句中的 *statements* 可能会被执行若干次,也可能不会被执行。
|
||||
|
||||
*condition* 表达式的值的类型必须遵循`LogicValue`协议。同时,*condition* 表达式也可以使用可选绑定,详情参见[可选绑定](../chapter2/01_The_Basics.html#optional_binding)。
|
||||
|
||||
> GRAMMAR OF A WHILE STATEMENT
|
||||
|
||||
> *while-statement* → **while** [*while-condition*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/while-condition) [*code-block*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/code-block)
|
||||
|
||||
> *while-condition* → [*expression*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Expressions.html#//apple_ref/swift/grammar/expression) | [*declaration*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/declaration)
|
||||
> While 循环语法
|
||||
> *while语句* → **while** [*while条件*](..\chapter3\10_Statements.html#while_condition) [*代码块*](..\chapter3\05_Declarations.html#code_block)
|
||||
> *while条件* → [*表达式*](..\chapter3\04_Expressions.html#expression) | [*声明*](..\chapter3\05_Declarations.html#declaration)
|
||||
|
||||
### Do-While 语句
|
||||
|
||||
@ -137,16 +110,13 @@ while `condition` {
|
||||
|
||||
`do-while`语句的形式如下:
|
||||
|
||||
```swift
|
||||
do {
|
||||
`statements`
|
||||
} while `condition`
|
||||
```
|
||||
> do {
|
||||
> `statements`
|
||||
> } while `condition`
|
||||
|
||||
`do-while`语句的执行流程如下:
|
||||
|
||||
1. 执行 *statements*,然后转到第2步。
|
||||
|
||||
2. 计算 *condition* 表达式:
|
||||
如果为`true`,转到第1步。如果为`false`,`do-while`至此执行完毕。
|
||||
|
||||
@ -154,9 +124,8 @@ do {
|
||||
|
||||
*condition* 表达式的值的类型必须遵循`LogicValue`协议。同时,*condition* 表达式也可以使用可选绑定,详情参见[可选绑定](../chapter2/01_The_Basics.html#optional_binding)。
|
||||
|
||||
> GRAMMAR OF A DO-WHILE STATEMENT
|
||||
|
||||
> *do-while-statement* → **do** [*code-block*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/code-block) **while** [*while-condition*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/while-condition)
|
||||
> Do-While 循环语法
|
||||
> *do-while语句* → **do** [*代码块*](..\chapter3\05_Declarations.html#code_block) **while** [*while条件*](..\chapter3\10_Statements.html#while_condition)
|
||||
|
||||
<a name="branch_statements"></a>
|
||||
## 分支语句
|
||||
@ -165,11 +134,9 @@ do {
|
||||
|
||||
`switch`语句中的控制流可以用`break`语句修改,详情请见[Break 语句](#break_statement)。
|
||||
|
||||
> GRAMMAR OF A BRANCH STATEMENT
|
||||
|
||||
> *branch-statement* → [*if-statement*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/if-statement)
|
||||
|
||||
> *branch-statement* → [*switch-statement*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/switch-statement)
|
||||
> 分支语句语法
|
||||
> *分支语句* → [*if语句*](..\chapter3\10_Statements.html#if_statement)
|
||||
> *分支语句* → [*switch语句*](..\chapter3\10_Statements.html#switch_statement)
|
||||
|
||||
### If 语句
|
||||
|
||||
@ -179,44 +146,35 @@ do {
|
||||
|
||||
第一种形式是当且仅当条件为真时执行代码,像下面这样:
|
||||
|
||||
```swift
|
||||
if `condition` {
|
||||
`statements`
|
||||
}
|
||||
```
|
||||
> if `condition` {
|
||||
> `statements`
|
||||
> }
|
||||
|
||||
第二种形式是在第一种形式的基础上添加 *else 语句*,当只有一个 else 语句时,像下面这样:
|
||||
|
||||
```swift
|
||||
if `condition` {
|
||||
`statements to execute if condition is true`
|
||||
} else {
|
||||
`statements to execute if condition is false`
|
||||
}
|
||||
```
|
||||
> if `condition` {
|
||||
> `statements to execute if condition is true`
|
||||
> } else {
|
||||
> `statements to execute if condition is false`
|
||||
> }
|
||||
|
||||
同时,else 语句也可包含`if`语句,从而形成一条链来测试更多的条件,像下面这样:
|
||||
|
||||
```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`
|
||||
}
|
||||
```
|
||||
> 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`
|
||||
> }
|
||||
|
||||
`if`语句中条件的值的类型必须遵循`LogicValue`协议。同时,条件也可以使用可选绑定,详情参见[可选绑定](../chapter2/01_The_Basics.html#optional_binding)。
|
||||
|
||||
> GRAMMAR OF AN IF STATEMENT
|
||||
|
||||
> *if-statement* → **if** [*if-condition*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/if-condition) [*code-block*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/code-block) [*else-clause*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/else-clause) *opt*
|
||||
|
||||
> *if-condition* → [*expression*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Expressions.html#//apple_ref/swift/grammar/expression) | [*declaration*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/declaration)
|
||||
|
||||
> *else-clause* → **else** [*code-block*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/code-block) | **else** [*if-statement*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/if-statement) *opt*
|
||||
> If语句语法
|
||||
> *if语句* → **if** [*if条件*](..\chapter3\10_Statements.html#if_condition) [*代码块*](..\chapter3\05_Declarations.html#code_block) [*else子句(Clause)*](..\chapter3\10_Statements.html#else_clause) _可选_
|
||||
> *if条件* → [*表达式*](..\chapter3\04_Expressions.html#expression) | [*声明*](..\chapter3\05_Declarations.html#declaration)
|
||||
> *else子句(Clause)* → **else** [*代码块*](..\chapter3\05_Declarations.html#code_block) | **else** [*if语句*](..\chapter3\10_Statements.html#if_statement)
|
||||
|
||||
### Switch 语句
|
||||
|
||||
@ -224,19 +182,17 @@ else {
|
||||
|
||||
`switch`语句的形式如下:
|
||||
|
||||
```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`
|
||||
}
|
||||
```
|
||||
> 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`
|
||||
> }
|
||||
|
||||
`switch`语句的*控制表达式(control expression)*会首先被计算,然后与每一个 case 的模式(pattern)进行匹配。如果匹配成功,程序将会执行对应的 case 分支里的 *statements*。另外,每一个 case 分支都不能为空,也就是说在每一个 case 分支中至少有一条语句。如果你不想在匹配到的 case 分支中执行代码,只需在该分支里写一条`break`语句即可。
|
||||
|
||||
@ -262,25 +218,16 @@ case let (x, y) where x == y:
|
||||
|
||||
当匹配的 case 分支中的代码执行完毕后,程序会终止`switch`语句,而不会继续执行下一个 case 分支。这就意味着,如果你想执行下一个 case 分支,需要显式地在你需要的 case 分支里使用`fallthrough`语句。关于`fallthrough`语句的更多信息,详情参见 [Fallthrough 语句](#fallthrough_statement)。
|
||||
|
||||
> GRAMMAR OF A SWITCH STATEMENT
|
||||
|
||||
> *switch-statement* → **switch** [*expression*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Expressions.html#//apple_ref/swift/grammar/expression) **{** [*switch-cases*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/switch-cases) *opt* **}**
|
||||
|
||||
> *switch-cases* → [*switch-case*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/switch-case) [*switch-cases*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/switch-cases) *opt*
|
||||
|
||||
> *switch-case* → [*case-label*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/case-label) [*statement*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/statements) | [*default-label*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/default-label) [*statements*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/statements)
|
||||
|
||||
> *switch-case* → [*case-label*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/case-label) **;** | [*default-label*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/default-label) **;**
|
||||
|
||||
> *case-label* → **case** [*case-item-list*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/case-item-list) **:**
|
||||
|
||||
> *case-item-list* → [*pattern*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Patterns.html#//apple_ref/swift/grammar/pattern) [*guard-clause*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/guard-clause) *opt* | [*pattern*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Patterns.html#//apple_ref/swift/grammar/pattern) [*guard-clause*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/guard-clause) *opt*, [*case-item-list*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/case-item-list)
|
||||
|
||||
> *default-label* → **default :**
|
||||
|
||||
> *guard-clause* → **where** [*guard-expression*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/guard-expression)
|
||||
|
||||
> *guard-expression* → [*expression*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Expressions.html#//apple_ref/swift/grammar/expression)
|
||||
> Switch语句语法
|
||||
> *switch语句* → **switch** [*表达式*](..\chapter3\04_Expressions.html#expression) **{** [*SwitchCase列表*](..\chapter3\10_Statements.html#switch_cases) _可选_ **}**
|
||||
> *SwitchCase列表* → [*SwitchCase*](..\chapter3\10_Statements.html#switch_case) [*SwitchCase列表*](..\chapter3\10_Statements.html#switch_cases) _可选_
|
||||
> *SwitchCase* → [*case标签*](..\chapter3\10_Statements.html#case_label) [*多条语句(Statements)*](..\chapter3\10_Statements.html#statements) | [*default标签*](..\chapter3\10_Statements.html#default_label) [*多条语句(Statements)*](..\chapter3\10_Statements.html#statements)
|
||||
> *SwitchCase* → [*case标签*](..\chapter3\10_Statements.html#case_label) **;** | [*default标签*](..\chapter3\10_Statements.html#default_label) **;**
|
||||
> *case标签* → **case** [*case项列表*](..\chapter3\10_Statements.html#case_item_list) **:**
|
||||
> *case项列表* → [*模式*](..\chapter3\07_Patterns.html#pattern) [*guard-clause*](..\chapter3\10_Statements.html#guard_clause) _可选_ | [*模式*](..\chapter3\07_Patterns.html#pattern) [*guard-clause*](..\chapter3\10_Statements.html#guard_clause) _可选_ **,** [*case项列表*](..\chapter3\10_Statements.html#case_item_list)
|
||||
> *default标签* → **default** **:**
|
||||
> *guard-clause* → **where** [*guard-expression*](..\chapter3\10_Statements.html#guard_expression)
|
||||
> *guard-expression* → [*表达式*](..\chapter3\04_Expressions.html#expression)
|
||||
|
||||
<a name="labeled_statement"></a>
|
||||
<a name="control_transfer_statements"></a> 带标签的语句
|
||||
@ -291,38 +238,28 @@ case let (x, y) where x == y:
|
||||
|
||||
关于使用带标签的语句的例子,详情参见[控制流](../chapter2/05_Control_Flow.html)一章的[带标签的语句](../chapter2/05_Control_Flow.html#labeled_statements)。
|
||||
|
||||
> GRAMMAR OF A LABELED STATEMENT
|
||||
|
||||
> *labeled-statement* → [*statement-label*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/statement-label) [*loop-statement*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/loop-statement) | [*statement-label*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/statement-label) [*switch-statement*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/switch-statement)
|
||||
|
||||
> *statement-label* → [*label-name*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/label-name) **:**
|
||||
|
||||
> *label-name* → [*identifier*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/LexicalStructure.html#//apple_ref/swift/grammar/identifier)
|
||||
|
||||
> 标记语句语法
|
||||
> *标记语句(Labeled Statement)* → [*语句标签*](..\chapter3\10_Statements.html#statement_label) [*循环语句*](..\chapter3\10_Statements.html#loop_statement) | [*语句标签*](..\chapter3\10_Statements.html#statement_label) [*switch语句*](..\chapter3\10_Statements.html#switch_statement)
|
||||
> *语句标签* → [*标签名称*](..\chapter3\10_Statements.html#label_name) **:**
|
||||
> *标签名称* → [*标识符*](..\chapter3\02_Lexical_Structure.html#identifier)
|
||||
|
||||
## 控制传递语句
|
||||
|
||||
通过无条件地把控制权从一片代码传递到另一片代码,控制传递语句能够改变代码执行的顺序。Swift 提供四种类型的控制传递语句:`break`语句、`continue`语句、`fallthrough`语句和`return`语句。
|
||||
|
||||
> GRAMMAR OF A CONTROL TRANSER STATEMENT
|
||||
|
||||
> *control-transfer-statement* → [*break-statement*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/break-statement)
|
||||
|
||||
> *control-transfer-statement* → [*continue-statement*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/continue-statement)
|
||||
|
||||
> *control-transfer-statement* → [*fallthrough-statement*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/fallthrough-statement)
|
||||
|
||||
> *control-transfer-statement* → [*return-statement*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/return-statement)
|
||||
> 控制传递语句(Control Transfer Statement) 语法
|
||||
> *控制传递语句* → [*break语句*](..\chapter3\10_Statements.html#break_statement)
|
||||
> *控制传递语句* → [*continue语句*](..\chapter3\10_Statements.html#continue_statement)
|
||||
> *控制传递语句* → [*fallthrough语句*](..\chapter3\10_Statements.html#fallthrough_statement)
|
||||
> *控制传递语句* → [*return语句*](..\chapter3\10_Statements.html#return_statement)
|
||||
|
||||
<a name="break_statement"></a>
|
||||
### Break 语句
|
||||
|
||||
`break`语句用于终止循环或`switch`语句的执行。使用`break`语句时,可以只写`break`这个关键词,也可以在`break`后面跟上标签名(label name),像下面这样:
|
||||
|
||||
```swift
|
||||
break
|
||||
break `label name`
|
||||
```
|
||||
> break
|
||||
> break `label name`
|
||||
|
||||
当`break`语句后面带标签名时,可用于终止由这个标签标记的循环或`switch`语句的执行。
|
||||
|
||||
@ -332,19 +269,16 @@ break `label name`
|
||||
|
||||
关于使用`break`语句的例子,详情参见[控制流](../chapter2/05_Control_Flow.html)一章的 [Break](../chapter2/05_Control_Flow.html#break) 和[带标签的语句](../chapter2/05_Control_Flow.html#labeled_statements)。
|
||||
|
||||
> GRAMMAR OF A BREAK STATEMENT
|
||||
|
||||
> *break-statement* → **break** [*label-name*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/label-name) *opt*
|
||||
> Break 语句语法
|
||||
> *break语句* → **break** [*标签名称*](..\chapter3\10_Statements.html#label_name) _可选_
|
||||
|
||||
<a name="continue_statement"></a>
|
||||
### Continue 语句
|
||||
|
||||
`continue`语句用于终止循环中当前迭代的执行,但不会终止该循环的执行。使用`continue`语句时,可以只写`continue`这个关键词,也可以在`continue`后面跟上标签名(label name),像下面这样:
|
||||
|
||||
```swift
|
||||
continue
|
||||
continue `label name`
|
||||
```
|
||||
> continue
|
||||
> continue `label name`
|
||||
|
||||
当`continue`语句后面带标签名时,可用于终止由这个标签标记的循环中当前迭代的执行。
|
||||
|
||||
@ -356,9 +290,8 @@ continue `label name`
|
||||
|
||||
关于使用`continue`语句的例子,详情参见[控制流](../chapter2/05_Control_Flow.html)一章的 [Continue](../chapter2/05_Control_Flow.html#continue) 和[带标签的语句](../chapter2/05_Control_Flow.html#labeled_statements)。
|
||||
|
||||
> GRAMMAR OF A CONTINUE STATEMENT
|
||||
|
||||
> *continue-statement* → **continue** [*label-name*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/label-name) *opt*
|
||||
> Continue 语句语法
|
||||
> *continue语句* → **continue** [*标签名称*](..\chapter3\10_Statements.html#label_name) _可选_
|
||||
|
||||
<a name="fallthrough_statement"></a>
|
||||
### Fallthrough 语句
|
||||
@ -369,9 +302,8 @@ continue `label name`
|
||||
|
||||
关于在`switch`语句中使用`fallthrough`语句的例子,详情参见[控制流](../chapter2/05_Control_Flow.html)一章的[控制传递语句](../chapter2/05_Control_Flow.html#control_transfer_statements)。
|
||||
|
||||
> GRAMMAR OF A FALLTHROUGH STATEMENT
|
||||
|
||||
> *continue-statement* → **fallthrough**
|
||||
> Fallthrough 语句语法
|
||||
> *fallthrough语句* → **fallthrough**
|
||||
|
||||
### Return 语句
|
||||
|
||||
@ -379,15 +311,12 @@ continue `label name`
|
||||
|
||||
使用`return`语句时,可以只写`return`这个关键词,也可以在`return`后面跟上表达式,像下面这样:
|
||||
|
||||
```swift
|
||||
return
|
||||
return `expression`
|
||||
```
|
||||
> return
|
||||
> return `expression`
|
||||
|
||||
当`return`语句后面带表达式时,表达式的值将会返回给调用者。如果表达式值的类型与调用者期望的类型不匹配,Swift 则会在返回表达式的值之前将表达式值的类型转换为调用者期望的类型。
|
||||
|
||||
而当只写`return`时,仅仅是将控制权从该函数或方法传递给调用者,而不返回一个值。(这就是说,该函数或方法的返回类型为`Void`或`()`)
|
||||
|
||||
> GRAMMAR OF A RETURN STATEMENT
|
||||
|
||||
> *return-statement* → **return** [*expression*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Expressions.html#//apple_ref/swift/grammar/expression) *opt*
|
||||
> Return 语句语法
|
||||
> *return语句* → **return** [*表达式*](..\chapter3\04_Expressions.html#expression) _可选_
|
||||
|
||||
Reference in New Issue
Block a user