《Swift 基礎教程2nd》迴圈和控制結構

來源:互聯網
上載者:User

標籤:ima   image   swift   default   todo   var   運算   控制結構   idt   

  • 流程式控制制

1  在特定的範圍內計數迴圈,結構為

for loopVar in startNumber...endNumber

 關鍵字in 的後面依次為起始數字,三個句點和結束數字,樣本:

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

 2 變種文法

for loopCount in 1..<10 {//print }

 3 老式for迴圈

for loopCount=0;loopCount<10;loopCount++) {//do something}

 開始使用playground

流程式控制制就是決策

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

 swift 比較子

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

 

if  true /false {//do sth1}else if true /false {//do sth2}else if true /false {//do sth3}else {//do oter thing}

 switch case default 類似C,但是不限於整形數字和enum,還可以用於String, 另外switch-case 在swift中不需要使用break

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

while 迴圈

while someCondition {//do sth}repeat {//do sth} while someCondition

 跳出迴圈使用break

//Todo。swift中怎麼沒有continue呢,如果僅僅退出當前迴圈呢?

 

《Swift 基礎教程2nd》迴圈和控制結構

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.