Swift if Statement
A if statement if statement consists of a Boolean expression followed by one or more statements.
Syntax
In the Swift language if statement syntax:
if boolean_expression {
/* statement to execute if the boolean expression is true */
}
If the Boolean expression is true, then the code block inside the if statement is executed. If the Boolean expression is false, then the first set of code after the if statement ends (after the closing brace) is executed.
Flowchart
Example
import Cocoa
var varA:Int = 10;
/* Check conditions */
if varA < 20 {
/* 如果条件语句为 true 执行以下程序 */
print("varA 小于 20");
}
print("varA 变量的值为 \(varA)");
When the code above is compiled and executed, it produces the following result:
varA 小于 20
varA 变量的值为 10
Swift Conditional Statements