Swift學習——使用if和switch來進行條件操作,使用for,while,和do-while來進行迴圈(三)

來源:互聯網
上載者:User

標籤:switch   測試   類   x   it   

Swift學習——使用if和switch來進行條件操作,使用for,while,和do-while來進行迴圈


//switch支援任意類型的資料以及各種比較操作——不僅僅是整數以及測試相等

//注意如果去掉default程式會報錯

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)")}


簡單的while和do-while迴圈


// while迴圈var n = 3while n < 100{    n = n*2}println("n is \(n)")//do-while迴圈var m = 3do{    m = m*2}while m < 100println("m is \(m)")

列印結果

stringsComment is say hello2 and hello3

n is 192

m is 192



相關文章

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.