swift 有4个 控制转移句: continue , break, fall through, return。
- continue:结束本次循环,执行下个循环,不跳出循环。
let puzzleInput = "hello world“
var puzzleOutput = ""
for character in puzzleInput {
switch character {
case "a", "e", "i", "u", " " :
continue
default:
puzzleOutput += character
}
}
打印结果: hllwrld
2. break:立即结束整个流程控制语句执行。
let numberSymbol: Character = "三"
var possibleIntegerValue: Int?
switch numberSymbol {
case "1","١", "一","๑":
possibleIntegerValue = 1
possibleIntegerValue = 2
case "2","٢", "二","๒":
case "3" , " ٣ " , " 三 " , " ๓ " :
possibleIntegerValue =3
case "4" , " ٤ " , " 四 " , " ๔ " :possibleIntegerValue = 4
default :
println ( "The integer value of \(numberSymbol) is \(integerValue) ." )
break
}
iflet integerValue = possibleIntegerValue {
} else {
println ( "An integer value could not be found for \(numberSymbol) " )
}
}
3.Fallthrough:在switch中,在不判断下个case的条件下,执行下个case里的语句
let integerToDescribe =5
var description = "The number \(integerToDescribe) is"
switch integerToDescribe {case 2 , 3 , 5 , 7 , 11 , 13 , 17 , 19 :
description += " a prime number, and also"
fallthrough
default :
description += " an integer"
}
println(description)
打印结果:The number 5 is a prime number, and also an integer
4.return: 是方法的返回。