- 快速教程
 - 斯威夫特 - 主页
 - 斯威夫特 - 概述
 - Swift - 环境
 - Swift - 基本语法
 - Swift - 数据类型
 - Swift - 变量
 - Swift - 可选
 - Swift - 元组
 - Swift - 常量
 - Swift - 文字
 - Swift - 运算符
 - Swift - 决策
 - Swift - 循环
 - Swift - 字符串
 - 斯威夫特 - 角色
 - Swift - 数组
 - Swift - 套装
 - 斯威夫特 - 字典
 - Swift - 函数
 - Swift - 闭包
 - Swift - 枚举
 - Swift - 结构
 - Swift - 类
 - Swift - 属性
 - Swift - 方法
 - Swift - 下标
 - Swift - 继承
 - Swift - 初始化
 - Swift - 去初始化
 - Swift - ARC 概述
 - Swift - 可选链接
 - Swift - 类型转换
 - Swift - 扩展
 - Swift - 协议
 - Swift - 泛型
 - Swift - 访问控制
 
- 斯威夫特有用的资源
 - Swift - 在线编译
 - Swift - 快速指南
 - Swift - 有用的资源
 - 斯威夫特 - 讨论
 
Swift - if...else if...else 语句
if语句后面可以跟一个可选的else if...else语句,这对于使用单个 if...else if 语句测试各种条件非常有用。
使用if、else if、else语句时,需要记住以下几点。
if可以有零个或一个else ,并且它必须位于任何 else if 之后。
一个if可以有零到多个else if,并且它们必须出现在 else 之前。
一旦else if成功,则不会测试其余的else if或else 。
句法
Swift 4 中if...else if...else语句的语法如下 -
if boolean_expression_1 {
   /* Executes when the boolean expression 1 is true */
} else if boolean_expression_2 {
   /* Executes when the boolean expression 2 is true */
} else if boolean_expression_3 {
   /* Executes when the boolean expression 3 is true */
} else {
   /* Executes when the none of the above condition is true */
}
例子
var varA:Int = 100;
/* Check the boolean condition using if statement */
if varA == 20 {
   /* If condition is true then print the following */
   print("varA is equal to than 20");
} else if varA == 50 {
   /* If condition is true then print the following */
   print("varA is equal to than 50");
} else {
   /* If condition is false then print the following */
   print("None of the values is matching");
}
print("Value of variable varA is \(varA)");
当上面的代码被编译并执行时,它会产生以下结果 -
None of the values is matching Value of variable varA is 100
swift_decision_making.htm