SoFunction
Updated on 2025-03-10

Basic learning notes on conditional judgment, loop and jump statements in Swift

1. Introduction

The power of a programming language depends largely on the program flow control scheme it provides, just as it is painful to implement complex program flows using assembly language. Swift provides many powerful process control statements, such as fast traversal for-in, while loop, repeat-while loop, switch selection, etc. It should be noted that in Swift 2.2, the for(a;b;c) loop has been deprecated, and the Switch statements in Swift are also more powerful and can handle any data type.

2. For-in loop

With the range operator, the for-in loop can be used to execute a loop that determines the number of times. The example is as follows:

for index in 1...5 {
  print(index)
}
//If you do not need to obtain the number of cycles in the loop, you can use the following methodvar sum=0;
for _ in 1...3 {
  sum += 1
}

For-in loops are also usually used to traverse arrays, dictionaries, collections, etc. The examples are as follows:

var collection1:Array = [1,2,3,4]
var collection2:Dictionary = [1:1,2:2,3:4,4:4]
var collection3:Set = [1,2,3,4]
for obj in collection1 {
  print(obj)
}
for (key , value) in collection2 {
  print(key,value)
}
for obj in collection3 {
  print(obj)
}

3. While loop

While statements perform loop operations until the loop condition is false. This type of loop is usually suitable for loop requirements with varying number of loops. While loops provide two syntax formats, the examples are as follows:

var i=0
// When i is not less than 10, the loop will jump outwhile i<10 {
  print("while",i)
  i+=1
}
//Perform an operation first and judge the cycle conditionsrepeat {
  print("repeat while")
} while i<10

4. If statement

If statement is one of the most commonly used statements in program development. If statements are used to determine whether a condition is true to perform process control of the program. If statements are usually used in combination with the else statement. The examples are as follows:

var c:Int
if 1>2 {
  c=1
}else if 1<0 {
  c=2
}else{
  c=3
}

5. Switch statement

Switch statements are used as switch selection statements to process branch selections of a set of values. Switch statements in Swift are particularly powerful. Compared with Objective-C, Switch statements in Swift do not need to be interrupted manually by break after each case. When the code matches a case, the statement will be interrupted by itself. The usage example code is as follows:

var charac:Character = "b"
//Use switch statement for character branch judgmentswitch charac {
case "a":
  print("chara is a")
case "b":
  print("chara is b")
case "c":
  print("chara is c")
default ://default is used to handle other additional situations  print("no charac")
}
//The same case can contain multiple branchesswitch charac {
case "a","b","c" :
  print("chara is word")
case "1","2","3" :
  print("chara is num")
default :
  print("no charac")
}
//A range can also be used in casevar num = 3
switch num {
case 1...3 :
  print("1&lt;=num&lt;=3")
case 4 :
  print("chara is num")
default :
  print("no charac")
}
//Use Switch statement for tuple matchingvar tuple = (0,0)
switch tuple {
case (0,1):
  print("Sure")
  //You can also match only one element in the tuplecase (_,1):
  print("Sim")
  //You can also perform range matching on elements in tuplecase(0...3,0...3):
  print("SIM")
default:
  print("")
}
//Do data bindingswitch tuple {
case (let a,1):
  print(a)
case (let b,0):
  print(b)
  //let(a,b) and (let a,let b) have the same meaningcase let(a,b):
  print(a,b)
default:
  print("")
}
//For Switch statements that have been data bound, you can use the where keyword to make conditional judgmentsswitch tuple {
case (let a,1):
  print(a)
case (let b,0):
  print(b)
//let(a,b) and (let a,let b) have the same meaningcase let(a,b) where a==b:
  print(a,b)
default:
  print("")
}

6. Jump statement

Swift provides 5 types of jump statements, continue, break, fallthrough, return, and throw.

: Jump out to the start position of the loop and start the next loop directly.

:break is directly interrupted in a loop statement and jumped out. If it is in a Switch structure, it will jump out of the Switch structure immediately.

The statement needs to be used in conjunction with the switch statement. If you use fallthrough in the case, the next case will continue to be executed. It should be noted that there is data binding in the next case, and fallthrough cannot be used. The example is as follows:

var tuple = (0,0)
switch tuple {
case (0,0):
  print("Sure")
  //fallthrough will continue to execute the following case  fallthrough
  //You can also match only one element in the tuplecase (_,0):
  print("Sim")
  fallthrough
  //You can also perform range matching on elements in tuplecase(0...3,0...3):
  print("SIM")
default:
  print("")
}

:The return statement returns directly from the function.

: throw is used to throw exceptions.

Swift also supports another syntax, which can set a tip tag for the while loop, and use keywords such as break and continue to control the process. The example is as follows:

var tmp = 0;
tip:while tmp<10 {
  print("ccc")
  tmp+=1
  switch tmp {
  case 3:
    break tip
  default:
    break
  }
}

After Swift 2.0, a new syntax is provided, guard-else, which is also called a guard statement. The code behind else is executed only when the condition is not met. The example is as follows:

var name = "HS"
func nameChange(name:String) {
  guard name=="HS" else{
    print(name)
    return
  }
  print("name is HS")
}

nameChange(name)

In development, functions often need to check whether the passed parameters meet the standards. The guard-else statement is created for this requirement. As its name suggests, it is used to protect the accuracy of the execution of the daemon function.

7. System version check

Use the following sample code to check the system supported version:

if #available(iOS 9, *){
  print("iOS 9")
}