Swift 嵌套的 If 语句

Swift 嵌套的 If 语句

在 Swift 4 中,嵌套 if-else 语句是合法的,这意味着您可以使用一个 ifelse if 来支持另一个 ifelse 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 */
   }
}
Swift

你可以以类似的方式嵌套 else if…else ,就像你嵌套if语句一样。

示例

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)");
Swift

当编译并执行以上代码时,会产生以下结果 –

First condition is satisfied
Second condition is also satisfied
Value of variable varA is 100
Value of variable varB is 200
Swift

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程

登录

注册