Logical branches and cyclic bodies of Swift study Notes

  • 2020-06-07 05:23:40
  • OfStack

Introduction to Branches

The branch is the judgment statement such as if/switch/3 mesh operator The execution flow of the program can be controlled through branch statements

1.if

OC

The latter condition must be added () And then we have the condition that if it's not 0 it's true If there is only 1 if, the brace after it can be omitted

if(a>0)NSlog(@"yes");

Swift

if without parentheses The if condition must be of the explicit Bool type Even if there is only one instruction, if, the curly braces must not be omitted

if else is used in the same way as OC1, except that there are no parentheses after the conditional statement. 3. Order operator and OC base 1;

2.guard

guard is a new syntax for swift2.0

Much like the if statement, it is designed to improve the readability of the program

The guard statement must be accompanied by an else statement, with the following syntax

When the conditional expression is true, the content in the else statement is skipped and the statement group content is executed

When the conditional expression is false, execute else statement content, is return jump statements 1, break, continue, throw


 // if 1 Six adults can only surf the Internet with their province id CARDS 
 func cherk(age:Int hasCard:Bool){
  if age >= 18{
   if hasCard{
   print(" The boss , A machine ")
   }else{
   print(" Get your ID card at home ")
   }
  else{
   print(" Minors cannot access the Internet ")
  }
 }
 func cherkGuard(age:Int hasCard:Bool){
  guard age >= 18 else {
  print(" The boss , A machine ")
  return
  }
  guard hasCard else {
  print(" Go home and get your province card ")
  return
  }
  print(" Minors cannot access the Internet ")
}

2. Basic use of switch

In the OC

switch the latter condition must be added () case can only be followed by 1 condition case will have a penetrating effect You can leave out default default can be placed anywhere The variables defined in case need to be bracketed, otherwise the scope will be confused You cannot judge objects or floating-point types, only integers

In the Swift

switch condition can not be added () case can be followed by multiple conditions separated by commas case will not have a penetrating effect. fallthrough will be added after penetrating You can't leave out default The default position must be placed last You do not need to enlarge parentheses to define variables in case Can judge object or floating-point type, can only judge integer

3. Special use of switch

Range match

Interval concept:

This is usually described as a numeric interval

Half open half closed interval

Closed interval


let range = 0...10// The closed interval represents the interval 0~10
let range1 = 0..<10 A half-open and half-closed interval represents an interval 0~9

Interval operation:

Intersection: clamped Overlap or not :overipas The judgment contains :contains Null or not :isEmpty

A tuple matching


let point = (10,15)
switch point{
  case (0,0)
  print(" Coordinates at the origin ")
  case (1...10,1...10)// An interval may be added to the primitive 
  print(" coordinates X,Y in 1...10 between ")
  case (,0)// An interval may be added to the primitive 
  print(" coordinates X in X On the shaft ")
  default 
  print(" other ") 
 }

Value binding


let point = (10,15)
switch point{
  case (var x,0)
  print("x=\(x)")//point In the x Assigned to x
  case (10,var y)
 print("y=\(y)")//point In the y Assigned to y
  case var(x,y)
  print("x=\(x) y=\(y)")//point In the xy Assigned to xy
  default 
  print(" other ")
}

Conditional binding


let point = (100,10)
switch point{
  // only where The following conditional statement expression assigns a value to a property and executes it case The following statement 
  case var( x,y) where x > y
  print("x=\(x) y=\(y)")
  default 
  print(" other ")
}

3. for cycle

Range of writing


 for _ in 0..<10{ //_ Is for neglect 
 print("xxx") 
 }

4.while cycle and do while cycle

while cycle


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

do while cycle


repeat {//swift  Not in the do ,do in swift Have special meaning , Used to catch exceptions 
 i +=1
 print(i)
}while i < 10

conclusion


Related articles: