Swift-nested-if-statement
提供:Dev Guides
Swift-ネストされたIfステートメント
Swift 4では、 if-else ステートメントをネストすることは常に有効です。つまり、1つの if または* else if を別の *if または else if ステートメントと一緒に使用できます。
構文
- ネストされたif *ステートメントの構文は次のとおりです-
if boolean_expression_1 {
/*Executes when the boolean expression 1 is true*/
if boolean_expression_2 {
/*Executes when the boolean expression 2 is true*/
}
}
_if_ステートメントをネストしたのと同様の方法で、 else if … else をネストできます。
例
var varA:Int = 100;
var varB:Int = 200;
/*Check the boolean condition using if statement*/
if varA == 100 {
/*If condition is true then print the following*/
print("First condition is satisfied");
if varB == 200 {
/*If condition is true then print the following*/
print("Second condition is also satisfied");
}
}
print("Value of variable varA is \(varA)");
print("Value of variable varB is \(varB)");
上記のコードをコンパイルして実行すると、次の結果が生成されます-
First condition is satisfied
Second condition is also satisfied
Value of variable varA is 100
Value of variable varB is 200