if...else statement
优质
小牛编辑
138浏览
2023-12-01
if语句后面可以跟一个可选的else语句,该语句在布尔表达式为false时执行。
语法 (Syntax)
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 block代码if block ,否则将执行代码else block 。
例子 (Example)
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