Swift 有三种条件判断:
三元运算
if 语句 后可以有可选的 else 语句, else 语句在布尔表达式为 false 时执行。
let level = 9001
if level > 9000 {
print("It's over 9000! You can proceed with your quest.")
} else {
print("Bummer, you definitely need to go for a training arc")
}
switch 语句允许测试一个变量等于多个值时的情况。
let level = 10000
switch level {
case 7000 :
print("Muda Muda Muda Muda Muda Muda")
case 8000 :
print("You are not strong enough")
case 9000 :
print("You are strong enough! You can proceed with your quest")
case 10000:
print("It's over 9000! You can proceed with your quest.")
default :
print("Please try again")
}
switch 语句允许测试一个变量等于多个值时的情况。 Swift 语言中 只要匹配到 case 语句,则整个 switch 语句执行完成。
let level = 9000
print( level > 9000 ? "It's over 9000! You can proceed with your quest." : print("Bummer, you definitely need to go for a training arc")