Swift If… Else语句
一个 if 语句后面可以跟随一个可选的 else 语句,当布尔表达式为false时执行。
语法
Swift 4中 if…else 语句的语法如下:
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代码块 ,否则执行 else代码块 。
示例
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