Latest web development tutorials

Swift nested if statements

Swift conditional statement Swift conditional statement

In Swift language, you can use another if or else if statement in an if or else if statement.


grammar

Swift language nested if statements syntax:

if boolean_expression_1 {
   /* 当 boolean_expression_1 表达式 true 时执行 */
   if boolean_expression_2 {
      /* 当 boolean_expression_1 表达式 true 时执行 */
   }
}

You can nestelse if ... else, manner similar to nestedifstatements.

Examples

import Cocoa

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

/* 检测布尔条件 */
if varA == 100 {
   /* 如果条件为 true 执行以下语句 */
   print("第一个条件为 true");
	
   if varB == 200 {
      /* 如果条件为 true 执行以下语句 */
      print("第二个条件也是 true");
   } 
}
print("varA 变量的值为 \(varA)");
print("varB 变量的值为 \(varB)");

When the above code is compiled executed, it will produce the following results:

第一个条件为 true
第二个条件也是 true
varA 变量的值为 100
varB 变量的值为 200

Swift conditional statement Swift conditional statement