Swift Process Control

Source: Internet
Author: User

Swift uses process control statements in the same C language, such as if, for, for-in, while, do-while, switch, break, and continue. the switch statement of swift language automatically adds the break effect, so that you do not need to write break after no case. unlike C, if for while in swift does not have brackets to enclose expressions.

1: If statement. The IF statement only checks the logical value. The IF statement is different from the C language to check whether it is not 0.

var bFlag:Bool = trueif bFlag {    println("\(bFlag)")}

2: For statements. The usage of for statements is similar to that of C for statements.

 var index:Int = 0for index; index < 5; index++ {     println("\(index)\n")  }

3: the for-in statement is used to traverse multiple sets.

for item in 1..5 {    println("\(item) ")}

If you do not use a set element, you can use _ ignore

var iCount : Int = 1let iBase : Int = 2for _ in 1...3 {  iCount *=  iBase}println("\(iCount)")

Use for-in to traverse Arrays

let array = [1,3,5,7]for item in array {    println("\(item) ")}

For-in traversal dictionary

let dic  = ["key1":"value1", "key2":"value2", "key3":"value3"]for (key, value) in dic {    println("key:\(key) value:\(value) \n")}

4: while loop. While loop judge logical value

var bFlag:Bool = truevar iCount:Int = 0while bFlag{    iCount++    if iCount > 3 {        bFlag = false    }    println("\(iCount)\n")}

5: Do-while. While/do-while the loop control process is the same as the C language, but their judgment is logical value rather than the C language! 0.

var bFlag:Bool = truevar iCount:Int = 0do{    iCount++    if iCount > 3 {        bFlag = false    }    println("\(iCount)\n")}while bFlag

6: switch statement. The switch statement must be a complete statement, and the defualt statement must be at the end of all case statements. After each case, the switch statement automatically break. The case block does not allow empty statements. A case condition can use range matching. In addition, the case condition can be matched using tuples, And the tuples can use "_" to represent any value.

let iCount :Int = 3switch iCount{case 1...6:    println("in")case 7..9:    println("out")default:    println("def")}

 

let character: Character = "c"switch character{case "a", "b", "c" :    println("xx")    case "d", "e":    println("oo")default:    println("def")}

 

VaR somepointe = (0.0) Switch somepointe {Case (0, 0): println ("") Case (_, 2): println ("_, 2 ") // _ match in this case (1, 2): println ("1, 2") // No, breaddefault: println ("def ")}

 

The fallthrough keyword is to remove break in the switch statement so that the code can continue the next statement.

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.