Swift - If... Else 语句
-
简述
if 语句后面可以跟一个可选的 else 语句,当布尔表达式为假时执行。 -
句法
的语法 if...else Swift 4 中的语句如下 -if boolean_expression { /* statement(s) will execute if the boolean expression is true */ } else { /* statement(s) will execute if the boolean expression is false */ }
如果布尔表达式的计算结果为 true,那么 if block 的代码将被执行,否则 else block 的代码将被执行。 -
例子
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 less than 20"); } else { /* If condition is false then print the following */ print("varA is not less than 20"); } print("Value of variable varA is \(varA)");
当上面的代码被编译和执行时,它会产生以下结果 -varA is not less than 20 Value of variable varA is 100