Swift 中的 嵌套if 语句函数

首页 / Swift入门教程 / Swift 中的 嵌套if 语句函数

在SWIFT 4中嵌套If-Else语句始意味着您可以在另一条If或Else If语句旁边使用一条If或Else If语句。

nested if - 语法

嵌套if语句的语法如下所示:-

无涯教程网

if boolean_expression_1 {
   /* 如果满足条件1,则执行这里 */
   
   if boolean_expression_2 {
      /* 如果同时满足条件1和条件2,则执行这里 */
   }
}

您可以嵌套else if.else,方法与嵌套if语句类似。

nested if - 示例

var varA:Int=100;
var varB:Int=200;

/* 检测varA是否等于100 */
if varA == 100 {
   /* 如果等于,则执行这里 */
   print("First condition is satisfied");

   if varB == 200 {
      /* 如果varA等于100,同时varB等于200,则执行这里 */
      print("Second condition is also satisfied");
   }
}

print("Value of variable varA is\(varA)");
print("Value of variable varB is\(varB)");

编译并执行上述代码时,将生成以下结果-

链接:https://www.learnfk.comhttps://www.learnfk.com/swift/swift-nested-if-statement.html

来源:LearnFk无涯教程网

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

祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)

技术教程推荐

微服务架构实战160讲 -〔杨波〕

机器学习40讲 -〔王天一〕

Elasticsearch核心技术与实战 -〔阮一鸣〕

图解 Google V8 -〔李兵〕

正则表达式入门课 -〔涂伟忠〕

分布式系统案例课 -〔杨波〕

性能优化高手课 -〔尉刚强〕

Spring Cloud 微服务项目实战 -〔姚秋辰(姚半仙)〕

手把手带你写一个MiniSpring -〔郭屹〕

好记忆不如烂笔头。留下您的足迹吧 :)