if 語句包含一個(gè)布爾表達(dá)式,后跟一個(gè)或多個(gè)語句。
if 語句在 Swift 的語法如下:
if boolean_expression {
/* statement(s) will execute if the boolean expression is true */
}
如果布爾表達(dá)式的計(jì)算結(jié)果為 true,則 if 語句中代碼塊將被執(zhí)行。如果布爾表達(dá)式的值為 false,那么 if 語句到(右大括號(hào)后)結(jié)束的第一組碼,將被執(zhí)行。

import Cocoa
var varA:Int = 11;
/* Check the boolean condition using if statement */
if varA < 12 {
/* If condition is true then print the following */
println("varA is less than 12");
}
println("Value of variable varA is \(varA)");
當(dāng)我們將上面的程序在 playground 中運(yùn)行,我們得到以下結(jié)果。
varA is less than 12 Value of variable varA is 11