在SWIFT 4中嵌套If-Else语句始意味着您可以在另一条If或Else If语句旁边使用一条If或Else If语句。
嵌套if语句的语法如下所示:-
if boolean_expression_1 { /* 如果满足条件1,则执行这里 */ if boolean_expression_2 { /* 如果同时满足条件1和条件2,则执行这里 */ } }
您可以嵌套else if.else,方法与嵌套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
祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)
Spring Cloud 微服务项目实战 -〔姚秋辰(姚半仙)〕