Latest web development tutorials

Swift if ... else statement

Swift conditional statement Swift conditional statement

After aif statement with an optional else statement,else statement executes the Boolean expression is false.

grammar

Swift language syntaxif ... else statement:

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

If the Boolean expression istrue, then the code ifthe execution block. If the Boolean expression isfalse, then the code within the elseblock executed.

flow chart

C in the if ... else statement

Examples

import Cocoa

var varA:Int = 100;

/* 检测布尔条件 */
if varA < 20 {
   /* 如果条件为 true 执行以下语句 */
   print("varA 小于 20");
} else {
   /* 如果条件为 false 执行以下语句 */
   print("varA 大于 20");
}
print("varA 变量的值为 \(varA)");

When the above code is compiled executed, it will produce the following results:

varA 大于 20
varA 变量的值为 100

Swift conditional statement Swift conditional statement