Swift if statement
One if
a statement consists of a Boolean expression followed by one or more statements.
Grammar
In Swift language if
syntax of the statement:
if boolean_expression {
/* The statement to be executed if the Boolean expression is true */
}
If the Boolean expression is true
, then if
the block of code within the statement will be executed. If the Boolean expression is false
, then if
the first set of code at the end of the statement (after closing parentheses) is executed.
Flow chart
Example
import Cocoa
var varA:Int = 10;
/* detected condition */
if varA < 20 {
/* If the conditional statement is true, execute the following program */
print("VarA < 20");
}
print("The value of the varA variable is \(varA)");
When the above code is compiled and executed, it produces the following results:
varA < 20
The value of the varA variable is 10