Swift if Statement

Swift 条件语句Swift Conditional Statements

Anif statementconsists of a boolean expression followed by one or more statements.

Syntax

In the Swift language,ifthe syntax of the statement:

if boolean_expression {
   /* 如果布尔表达式为真将执行的语句 */
}

If the boolean expression istrue, then the code block inside the if statement will be executed. If the boolean expression isfalse, then the first set of code after the if statement (after the closing brace) will be executed.

Flowchart

C 中的 if 语句

Example

import Cocoa

var varA:Int = 10;

/* 检测条件 */
if varA < 20 {
    /* 如果条件语句为 true 执行以下程序 */
    print("varA 小于 20");
}
print("varA 变量的值为 \(varA)");

When the above code is compiled and executed, it will produce the following results:

varA 小于 20
varA 变量的值为 10

Swift 条件语句Swift Conditional Statements

Other Extensions