Learning notes on conditional judgment loops and jump statements in Swift

  • 2020-05-19 06:00:13
  • OfStack

1. The introduction

Whether a programming language is powerful or not depends largely on the program flow control scheme it provides, just as using assembly language to implement complex program flow is a painful thing. Many powerful process control statements are provided in Swift, such as quick traversal of for-in, while loops, repeat-while loops, switch options, etc. It is important to note that in Swift2.2, for(a; b; c) loops have been deprecated, and Switch statements in Swift are more powerful and can handle any data type.

2. for - in cycle

With the range operator, the for-in loop can be used to execute a loop a certain number of times, as shown below:


for index in 1...5 {
  print(index)
}
// If you don't need to get the number of loops in a loop   You can use the following 
var sum=0;
for _ in 1...3 {
  sum += 1
}

The for-in loop is also commonly used to traverse groups, dictionaries, collections, etc. Examples are:


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 cycle

The while statement loops until the loop condition is false. This type of loop is usually suitable for loops with varying number of loops. while loops provide two syntax formats, as shown in the following example:


var i=0
// when i Not less than 10 Break out of the loop when 
while i<10 {
  print("while",i)
  i+=1
}
// to 1 operations   In judging the loop condition 
repeat {
  print("repeat while")
} while i<10

4. if statements

if statement is one of the most commonly used statements in program development. if will judge whether a condition is valid to control the process of the program. if statement is usually used in combination with else statement.


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

5. Switch statements

Switch statement is used as a switch selection statement to process the branch selection of 1 group of values. Switch statement in Swift is particularly powerful. Compared with Objective-C, Switch statement in Swift does not need to use break for manual interrupt after each case. The usage sample code is as follows:


var charac:Character = "b"
// use switch Statement for character branching 
switch charac {
case "a":
  print("chara is a")
case "b":
  print("chara is b")
case "c":
  print("chara is c")
default ://default Used to handle other additional situations 
  print("no charac")
}
// with 1 a case Can contain multiple branches 
switch charac {
case "a","b","c" :
  print("chara is word")
case "1","2","3" :
  print("chara is num")
default :
  print("no charac")
}
// in case Can also be used in 1 A range of 
var num = 3
switch num {
case 1...3 :
  print("1<=num<=3")
case 4 :
  print("chara is num")
default :
  print("no charac")
}
// use Switch Statements match tuples 
var tuple = (0,0)
switch tuple {
case (0,1):
  print("Sure")
  // You can also match only one element in a tuple 
case (_,1):
  print("Sim")
  // You can also scope match elements in tuples 
case(0...3,0...3):
  print("SIM")
default:
  print("")
}
// Data binding 
switch tuple {
case (let a,1):
  print(a)
case (let b,0):
  print(b)
  //let(a,b)  with  (let a,let b) The same meaning 
case let(a,b):
  print(a,b)
default:
  print("")
}
// For data bound Switch statements   You can use where Keyword to make conditional judgment 
switch tuple {
case (let a,1):
  print(a)
case (let b,0):
  print(b)
//let(a,b)  with  (let a,let b) The same meaning 
case let(a,b) where a==b:
  print(a,b)
default:
  print("")
}

6. Jump statement

Five jump statements are available in Swift, continue, break, fallthrough, return, throw.

1.continue: jump out of the loop to start the next loop.

2.break: break breaks the loop if it is in a loop, or Switch if it is in Switch.

3.fallthrough statement needs to be used in conjunction with switch statement. If fallthrough is used in case, the next case will continue to be executed.


var tuple = (0,0)
switch tuple {
case (0,0):
  print("Sure")
  //fallthrough I'm going to go ahead and do the following case
  fallthrough
  // You can also match only one element in a tuple 
case (_,0):
  print("Sim")
  fallthrough
  // You can also scope match elements in tuples 
case(0...3,0...3):
  print("SIM")
default:
  print("")
}

4.return: return statement is returned directly from the function.

5.throw: throw is used to throw exceptions.

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


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

After Swift2.0, a new syntax, guard-else, is provided. This is also known as a daemon statement. The code behind else is executed only when the condition is not met.


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

nameChange(name)

The guard-else statement, as its name implies, is used for the precision with which the daemon function is executed.

7. System version check

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


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


Related articles: