Latest web development tutorials

Swift if statement

Swift conditional statement Swift conditional statement

If a Boolean expression followed by a statementby the one or more statements.

grammar

Swift languageif the syntax of the statement:

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

If the Boolean expression istrue, the if statement within a block of code will be executed.If the Boolean expression isfalse, if the first set of codes after the statement (after the close parenthesis) will be executed.

flow chart

C if statement

Examples

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 produces the following results:

varA 小于 20
varA 变量的值为 10

Swift conditional statement Swift conditional statement