目录

if statement

if语句由一个布尔表达式后跟一个或多个语句组成。

语法 (Syntax)

Swift 4中if语句的语法如下 -

if boolean_expression {
   /* statement(s) will execute if the boolean expression is true */
}

如果布尔表达式的计算结果为true ,那么将执行if语句中的代码块。 如果布尔表达式的计算结果为false ,则将执行if语句结束后(在结束大括号之后)的第一组代码。

流程图 (Flow Diagram)

如果声明

例子 (Example)

var varA:Int = 10;
/* Check the boolean condition using if statement */
if varA < 20 {
   /* If condition is true then print the following */
   print("varA is less than 20");
}
print("Value of variable varA is \(varA)");

当我们使用playground运行上述程序时,我们得到以下结果。

varA is less than 20
Value of variable varA is 10
↑回到顶部↑
WIKI教程 @2018