In layman Swift (2)--Control flow

Source: Internet
Author: User



In addition to having all C control flow structures, Swift also has the for...in in OC ... The structure facilitates traversal of arrays, dictionaries, and so on.






Cycle



The swift cycle provides four structures:



<1>for...in: Commonly used to iterate over arrays, similar to Forearch in C #





var myArray = ["string1",123,456]
for i in myArray{
   println("item is \(i)")
}

var myArray = ["v1":"string1","v2":123,"v2":11.34]
for (name,value) in myArray{
    println("key name=\(name)   key value=\(value)")
}






<2>for...condition...increment: This structure is the same as OC's for.





for var i=0; i<10; i++{
   println("index is \(i)")
}

<3>while: As with OC's while







while 1<2 {
  println("this is while loop")
}

<4>do...while: Same as OC's Do...while







do{
  println("this is do while")
}while 1<2






"Conditional Statement"



<1>if...else





if 1<2 {
  .......
}
else if 1<3{
  .......
}
else{
  .......
}







<2>switch


var num:int





switch num {
   case 1:
   case 2:
       println("num is 2")
   case 3:
      println("num is 3")
   case 4,5,6:
      println("num is 4 or 5 or 6")
   default:
     println("num is not 1 or 2 or 3")   
}









In layman Swift (2)--Control flow


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.