Swift Learning -- use if and switch for conditional operations, and use for, while, and do-while for loop (3)

Source: Internet
Author: User

Swift Learning -- use if and switch for conditional operations, and use for, while, and do-while for Loop



// Switch supports any type of data and various comparison operations-not only integers and test equality

// If the default program is removed, an error is returned.

let strings = "hello3"
switch strings{
    case "hello1":
    let stringsComment = "say hello1"
    println("stringsComment is \(stringsComment)")
    break
    case "hello2","hello3":
    let stringsComment = "say hello2 and hello3"
    println("stringsComment is \(stringsComment)")
    break
    case let x where x.hasSuffix("hello4"):
    let stringsComment = "Is it a spicy \(x)?"
    println("stringsComment is \(stringsComment)")
    break
    default:
    let stringsComment = "say everything"
    println("stringsComment is \(stringsComment)")
}


Simple while and do-while Loops



// while loop
var n = 3
while n <100 {
     n = n * 2
}
println ("n is \ (n)")

// do-while loop
var m = 3
do {
     m = m * 2
} while m <100

println ("m is \ (m)") 

Print results



Stringscomment is say hello2 and hello3

N is 192

M is 192



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.