Swift Control statements

Source: Internet
Author: User

1. Branch statements


(1) Conditional statement if


If


Else


If Else



(2) Multi-branch statement switch


A switch statement can use integers, floating-point numbers, characters, strings, and tuples, and its values can be discrete or continuous.


The case branch does not require an explicit add break statement


You can add multiple values after each case, and multiple values are separated by commas, and each switch must have a default statement that is placed behind all branches and at least one case statement


(3) using range matching in switch

Closed interval: ....

90...100 means 90>= =<100


Semi-closed interval:.. < 90..<100 means 90< <100




Use an underscore (_) in the case to ignore the field values in it:

var student= ("id": "1002", "Name": "John Doe", "Age": "+", "Chinesescore": $, "Englishscore": 90)


Switch student{

Case (_,_,_,90...100,90...100):

Descr= "Excellent"

Case (_,_,_,80...90,80...90):

Descr= "Good"

Case (_,_,_,60...80,60...80):

Descr= "Medium"

Case (_,_,_,60...80,90...100), (_,_,_,90...100,60...80):

descr= "partial section"

Case (_,_,_,0...60,90...100), (_,_,_,90...100,0...60):

descr= "Serious partial section"

Default

Descr= "None"


}



Use a comma to separate two tuple values, representing or relationships


(4) Comparing the value bindings of tuples in switch


When using tuples, you can bind matching values to a temporary variable or constant in the case branch, which can be used in a branch, which becomes a value binding


var student= ("id": "1002", "name": "Four", "Age": "+", "Chinesescore":, "Englishscore": 90)

Switch student{

Case (_,_,let age,90...100,90...100):

if (age > 30) {

Descr= "Old Man"

}

else{

Descr= "Kid"


}


}


(5) Comparing tuples in switch


When you bind a tuple value, you can also use the where statement in the case to filter the conditions, similar to where in the SQL statement


Switch student{

Case (_,_,let age,90...100,90...100) where the age >0:

Descr= "Excellent"




}





2. Circular statements


(1) While statement


While loop condition {

Statement Group


}


(2) Do While statement


do {


}while conditions


(3) For statement


for initialization; loop condition; Iteration {


}



(4) in statement




3. Jump Statement


(1) Break statement


Break can be applied to switch statements and loop statements


label1:for var x=0;x<5;x++{

label2:for var y=5;y>0;y--{

if (x==y) {

Break Label1


}

println ("(x.y) = (\ (x), \ (y))")


}


}


(2) Continue statement


(3) Fallthrough statement

Through

Let the loop statement continue down.


Case 3:


i++

Fallthrough


Case 4:

i++

Fallthrough






This article is from the "Ordinary Road" blog, please be sure to keep this source http://linjohn.blog.51cto.com/1026193/1619485

Swift Control statements

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.