Ben*_*wry 41
继续:
continue语句结束循环语句的当前迭代的程序执行,但不会停止循环语句的执行.
var sum = 0;
for var i = 0 ; i < 5 ; i++ {
if i == 4 {
continue //this ends this iteration of the loop
}
sum += i //thus, 0, 1, 2, and 3 will be added to this, but not 4
}
Run Code Online (Sandbox Code Playgroud)
打破:
break语句结束循环,if语句或switch语句的程序执行.
var sum = 0;
for var i = 0 ; i < 5 ; i++ {
if i == 2 {
break //this ends the entire loop
}
sum += i //thus only 0 and 1 will be added
}
Run Code Online (Sandbox Code Playgroud)
通电路:
fallthrough语句导致程序执行从switch语句中的一个case继续到下一个case.
var sum = 0
var i = 3
switch i {
case 1:
sum += i
case 2:
sum += i
case 3:
sum += i
fallthrough //allows for the next case to be evaluated
case i % 3:
sum += i
}
Run Code Online (Sandbox Code Playgroud)
扔:
您使用throw语句抛出错误.
您可能会抛出类似下面的错误,表示由于资金不足,还需要五个硬币.
throw VendingMachineError.InsufficientFunds(coinsNeeded: 5)
Run Code Online (Sandbox Code Playgroud)
返回:
return语句出现在函数或方法定义的主体中,并导致程序执行返回到调用函数或方法.
func myMethod(){
newMethod()
}
func newMethod(){
var sum = 0;
sum += 2;
if sum > 1 {
return //jumps back to myMethod()
}
sum += 3; //this statement will never be executed
}
Run Code Online (Sandbox Code Playgroud)
返回也可用于从函数返回值.
func myFunc() {
let value = newFunc() //assigns 5 to "value"
}
func newFunc() -> Int {
return 5
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
5988 次 |
最近记录: |