无涯教程-Swift - if...elseif...else 语句函数

77 阅读1分钟

If语句后面可以跟随可选的Else If.Else语句,这对于使用单个If.Else If语句测试各种条件判断非常有用。

if...else if...else - 语法

SWIFT 4中if.else语句的语法如下所示:-

if boolean_expression_1 {
   /* 当布尔表达式 1 为真时执行 */

} else if boolean_expression_2 { /* 当布尔表达式 2 为真时执行 */

} else if boolean_expression_3 { /* 当布尔表达式 3 为真时执行 */

} else { /* 都不满足,则执行这里 */ }

if...else if...else - 示例

var varA:Int=100;

/* 使用 if 语句检查布尔条件 / if varA == 20 { / 如果条件为真(varA等于20),则打印以下内容 */ print("varA is equal to than 20");

} else if varA == 50 { /* 如果条件为真(varA等于50),则打印以下内容 */ print("varA is equal to than 50");

} else { /* 否则执行这里 */ 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

参考链接

www.learnfk.com/swift/swift…