SoFunction
Updated on 2025-04-11

Logical branches and loops of Swift learning notes

Introduction to the branch

  • Branches, that is, if/switch/three-point operators, etc.
  • The execution process of the program can be controlled through branch statements

OC

  • The following conditions must be added ()
  • The following conditions are either 0 or true
  • If there is only one brace after an if it can be omitted
if(a>0)NSlog(@"yes");

Swift

  • If no brackets are added after
  • If the following conditions must be a clear Bool type
  • Even if there is only one brace after the instruction if, it cannot be omitted

If else is used consistent with OC, except that there is no bracket after the conditional statement; the tri-item operator and OC are basically consistent;

guard is a new syntax for swift2.0

Very similar to if statement, the design aims to improve the readability of the program

The guard statement must have an else statement, the syntax is as follows

When the conditional expression is true, skip the contents in the else statement and execute the statement group contents

When the conditional expression is false, the content of the else statement is executed. The jump statement is usually return, break, continue, throw

 //If an adult brings a provincial certificate, he can access the Internet func cherk(age:Int hasCard:Bool){
  if age >= 18{
   if hasCard{
   print("Boss, turn on the machine")
   }else{
   print("Get your ID card back home")
   }
  else{
   print("Minors cannot access the Internet")
  }
 }
 func cherkGuard(age:Int hasCard:Bool){
  guard age >= 18 else {
  print("Boss, turn on the machine")
  return
  }
  guard hasCard else {
  print("Get your ID card back home")
  return
  }
  print("Minors cannot access the Internet")
}

Basic use

In OC

  • The conditions after switch must be added ()
  • Only one condition can be followed by a case
  • The case will have a penetration effect
  • You can not write default
  • You can place the default location at will
  • Defining variables in case requires braces, otherwise the scope will be confused
  • Can't judge objects or floating point types, only integers

Swift

  • The conditions after switch can be added without adding ()
  • The case can be separated from multiple conditions by commas.
  • The case will not have a penetration effect. Add fallthrough after penetration.
  • You can't write default
  • The default position must be placed last
  • Defining variables in case without adding braces
  • Can judge objects or floating point types, only integers

Special use

Interval matching

Interval concept:

Usually described is the numerical interval

Half open and half closed range

Closed interval

let range = 0...10//Close interval represents interval 0~10let range1 = 0..<10Half open and half closed interval represents the interval0~9

Interval operation:

  • Intersection: clamped
  • Overlap: overipas
  • Judgment includes: contains
  • Is it empty?: isEmpty

Tuple Match

let point = (10,15)
switch point{
  case (0,0)
  print("Coordinates at the origin")
  case (1...10,1...10)//You can add intervals to the Yuanzu  print("Coordinates X, Y are between 1...10")
  case (,0)//You can add intervals to the Yuanzu  print("Coordinate X is on the X axis")
  default 
  print("other") 
 }

Value binding

let point = (10,15)
switch point{
  case (var x,0)
  print("x=\(x)")//The x in point is assigned to x  case (10,var y)
 print("y=\(y)")//The y in point is assigned to y  case var(x,y)
  print("x=\(x) y=\(y)")//The xy in point is assigned to xy  default 
  print("other")
}

Bind according to conditions

let point = (100,10)
switch point{
  //Only the conditional statement expression after where is assigned the true wealth and execute the statement after case  case var( x,y) where x > y
  print("x=\(x) y=\(y)")
  default 
  print("other")
}

cycle

Interval writing

 for _ in 0..<10{ //_ means ignore print("xxx") 
 }

Loop and do while loop

while loop

var i =10
while i > 0{
i-=1;
print (i)
}

do while loop

repeat {//Do does not use it in swift. Do has a special meaning in swift and is used to catch exceptions i +=1
 print(i)
}while i < 10

Summarize

The above is the entire content of this article. I hope that the content of this article has a certain reference value for everyone's study or work. If you have any questions, you can leave a message to communicate. Thank you for your support.