Swift if Statement
An if statement consists of a Boolean expression followed by one or more statements.
Syntax
The syntax for an if statement in Swift:
if boolean_expression {
/* Statements to be executed if the Boolean expression is true */
}
If the Boolean expression evaluates to true, the block of code inside the if statement is executed. If the Boolean expression evaluates to false, the first set of code after the closing bracket is executed.
Flowchart
Example
import Cocoa
var varA:Int = 10;
/* Check the condition */
if varA < 20 {
/* Execute the following code if the condition is true */
print("varA is less than 20");
}
print("The value of varA is \(varA)");
When the above code is compiled and executed, it produces the following result:
varA is less than 20
The value of varA is 10