if语句后面可以跟随可选的else语句,该语句在布尔表达式为false时执行。
SWIFT 4中if.else语句的语法如下所示:-
if boolean_expression { /* 如果满足条件则执行这里 */ } else { /* 否则执行这里 */ }
如果布尔表达式的计算结果为true,则将执行代码的if块,否则将执行代码的else块。
var varA:Int=100; /* 检测varA是否小于20 */ if varA < 20 { /* 如果小于20,则执行这里 */ print("varA is less than 20"); } else { /* 否则执行这里 */ print("varA is not less than 20"); } print("Value of variable varA is\(varA)");
编译并执行上述代码时,将生成以下结果-
链接:https://www.learnfk.comhttps://www.learnfk.com/swift/swift-if-else-statement.html
来源:LearnFk无涯教程网
varA is not less than 20 Value of variable varA is 100
祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)