Swift Basic Tutorial 2nd loop and Control structure

Source: Internet
Author: User
Tags integer numbers switch case


    • Process Control


1 counting loops within a specific range, structure for

for loopVar in startNumber ... endNumber
 The keyword in is followed by a starting number, three periods, and an ending number, for example:

var loopCount: Int = 0
for loopCount in 1 ... 10 {
print ("# \ (loopCount)")
}
 2 variant syntax

for loopCount in 1 .. <10 {
// print
}
 3 old fashioned for loop

for loopCount = 0; loopCount <10; loopCount ++) {
// do something
}
 Get started with playground

Process control is decision

if (true / false) {
// do sth
} else
{
// do other thing
}
 swift comparison operator

==,! =,>, <,> =, <=
 

if true / false {
// do sth1
} else if true / false {
// do sth2
} else if true / false {
// do sth3
} else {
// do oter thing
}
 switch case default is similar to C, but it is not limited to integer numbers and enums. It can also be used for String. In addition, switch-case does not require break in Swift

for three in threeArray {
  switch three {
    case "Cak":
      print ("Furniture")
    case "Pecan":
      print ("Pie")
     case "Maple":
       print ("Syrup")
    default:
       print ("Wood")
 }
}
while loop

while someCondition {
// do sth
}


repeat {
// do sth
} while someCondition
 Break out of the loop using break

// Todo. Why is there no continue in swift, if it just exits the current loop?

 

"Swift Basic Tutorial 2nd" Loops and Control Structures

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.