Swift - 嵌套的 If 语句

  • 简述

    在 Swift 4 中嵌套总是合法的 if-else 语句,这意味着您可以使用一个 if 要么 else if另一边 if 要么 else if 声明。
  • 句法

    的语法 nested 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 */
       }
    }
    
    你可以嵌套 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)");
    
    当上面的代码被编译和执行时,它会产生以下结果 -
    
    First condition is satisfied
    Second condition is also satisfied
    Value of variable varA is 100
    Value of variable varB is 200