Swift-if-else-statement
提供:Dev Guides
Swift-If … その他の声明
*if* ステートメントの後にオプションの *else* ステートメントを続けることができます。これは、ブール式がfalseの場合に実行されます。
構文
Swift 4の if … else ステートメントの構文は次のとおりです-
if boolean_expression {
/*statement(s) will execute if the boolean expression is true*/
} else {
/*statement(s) will execute if the boolean expression is false*/
}
ブール式が true と評価されると、コードの* ifブロック*が実行され、そうでない場合はコードの* elseブロック*が実行されます。
例
var varA:Int = 100;
/*Check the boolean condition using if statement*/
if varA < 20 {
/*If condition is true then print the following*/
print("varA is less than 20");
} else {
/*If condition is false then print the following*/
print("varA is not less than 20");
}
print("Value of variable varA is \(varA)");
上記のコードをコンパイルして実行すると、次の結果が生成されます-
varA is not less than 20
Value of variable varA is 100