Switch...case Statement Instance parsing in Swift programming _swift

Source: Internet
Author: User
Tags case statement

The Switch...case statement in Swift can determine the object type, and the objective-c must be an integer.
Can not penetrate, can not write break,

var rank = "A"
switch rank{case
  "a"://equivalent to if
    print ("excellent") Case
  "B"://equivalent to else if
    print ("excellent")
  case "C"://equivalent to else if
    print ("excellent")
  default://equivalent to else
    print ("no rating")
}


Because it can't penetrate, so it can't be written.

var rank1 = "A"
switch rank1{case
  "a": Case
  "B":
    print ("excellent") Case
  "C":
    print ("excellent")
  Default:
    print ("no rating")
}


That's the only way to write

var rank1 = "A"
switch rank1{case
  "a", "B"://Note OC cannot write
    print ("excellent")
  "C":
    print ("poor")
  Default:
    print ("no rating")
}

Cannot write default

var rank2 = "A"
switch rank2{case
  "a":
    print ("excellent") Case
  "B":
    print ("Liang") Case
  "C":
    print (" Bad ")
}

Default location can only be in the last

var rank3 = "A"
switch rank3{
  default:
    print ("no rating") Case
  "A":
    print ("excellent") Case
  "B":
    print ("Liang") Case
  "C":
    print ("Bad")
}

Define variables in case without adding parentheses

var rank4 = "A"
switch rank4{case
  "a":
    var num = ten
    print ("excellent") Case
  "B":
    print ("Liang") Case
  "C":
    print ("bad")
  default:
    print ("no rating")
}

Interval and Ganso matching

var num = ten;
Switch num{case
  1...9:
    print ("single digit") Case
  10...99:
    print ("10 digits")
  default:
    print ("other number ")
}
var point = (a)
switch point{case
  (0, 0):
    print ("Coordinate at origin") Case
  (1...10, 10...20)://Can be added in Yongzu interval 
   
    print ("coordinate x and y between 1~10") Case
  (_, 0)://X can be any number
    print ("Coordinate x on x-axis")
  default:
    print ("Other")
}


   

Value binding

var point = (1)
switch point{case
  (var x, 10)://assigns the value of x in point to X
    print ("x= \ (x)") Case
  (var x, V Ar y)://Will assign the value of XY in point to XY
    print ("x= \ (x) y= \ (y)") Case
  var (x, y):
    print ("x= \ (x) y= \ (y)")
  default :
    Print ("Other")
}

Binding based on criteria

var point = (
m) switch point{
  //Only the conditional expression after the where is assigned and executes the statement case
  var (x, Y) where x > y: 
    Print ("x= \ (x) y= \ (y)")
  default:
    print ("Other")
}

Fallthrough keyword
The switch in the swift language does not fall from the previous case branch into the next case branch. Instead, as soon as the first matching case branch completes the statement it needs to execute, the entire switch code block completes its execution. In contrast, the C language requires that you display the Insert break statement to the end of each switch branch to prevent automatic falling into the next case branch. The nature of the swift language's avoidance of default falling into the next branch means that its switch function is clearer and more predictable than the C language, and avoids the error caused by unconsciously executing multiple case branches.

If you do need a C-style fall (Fallthrough) feature, you can use the Fallthrough keyword in each case branch that requires that feature. The following example uses Fallthrough to create a descriptive statement for a number.

Let Integertodescribe = 5 
var description = "The number \ (Integertodescribe) is" 
switch integertodescribe { 
c ASE 2, 3, 5, 7, 
  description + = "A prime number, and also" 
  fallthrough 
default: 
  descriptio n + = "an integer." 
} 
println (description) 
//Prints "The number 5 is a prime number, and also an integer." 

The

example defines a string variable description and sets an initial value for it. function uses the switch logic to determine the value of a integertodescribe variable. When the value of Integertodescribe is one of the prime numbers in the list, the function adds a text after description to indicate that the number is a prime. It then uses the Fallthrough keyword to "fall" into the default branch. The default branch adds an extra text at the end of the description, and the switch code block is finished.
&NBSP
If the value of Integertodescribe does not belong to any prime number in the list, it does not match to the first switch branch. There are no other special branches here, so the Integertodescribe match to all of the default branches.
 
To print a description of the number using the PRINTLN function when the switch code block finishes executing. In this example, the number 5 is accurately identified for a prime. The
 
Note:fallthrough keyword does not check for matching criteria in the next case that will fall into execution. Fallthrough simply allows code execution to continue to connect to the execution code in the next case, which is the same as the switch statement feature in the C language standard.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.