1 // 2 // ViewController.swift 3 // Swift-循環語句 4 // 5 // Created by luorende on 16/12/08. 6 // Copyright © 2016年 luorende. All rights reserved. 7 // 8 9 import UIKit 10 11 class ViewController: UIViewController { 12 13 override func viewDidLoad() { 14 super.viewDidLoad() 15 // Do any additional setup after loading the view, typically from a nib. 16 17 18 // 循環語句能夠使程序代碼重復執行。Swift編程語言支持4種循環構造類型:while、do while、for和for in。for和while循環是在執行循環體之前測試循環條件,而do while是在執行循環體之后測試循環條件。這就意味着for和while循環可能連一次循環體都未執行,而do while將至少執行一次循環體。for in是for循環的變形,它是專門為集合遍歷而設計的。 19 20 21 // 一、 for 的用法 22 // 一般格式:for 變量;變量<某個值;變量++ 23 // 例子 1 24 for i in 0 ..< 5 { //從0開始到小於5 25 print("i=\(i)") 26 } 27 // 例子 2 Swift3.0會報錯,好像說是移除C的風格 28 // var j=0 //把變量寫在外面 29 // for j=0; j<5 ; j += 1 { 30 // print("j=\(j)") 31 // } 32 33 34 35 // 二、for in 的用法 36 // 格式:for 變量或臨時變量 in 集合 37 // 說明:當執行for in語句的時候,集合中所對應的值依次會賦給變量或臨時變量 38 // 例子 1 39 // 字符串遍歷: 40 var str = "ABCDEFG" 41 for temp in str.characters {//遍歷字符串里面的字符 42 print(" temp=\(temp) ") 43 } 44 45 46 47 48 // 例子 2 49 // 遍歷序列 50 let A = 1...5// ... 三個點表示序列 即 1到5的序列(1,2,3,4,5)也可以說是1-5的合集 51 for temp in A { 52 print("temp=\(temp)") 53 } 54 /** 55 運行結果: 56 temp=1 57 temp=2 58 temp=3 59 temp=4 60 temp=5 61 */ 62 63 64 65 // for 循環遍歷int整形數組的里的個數 66 let numbers = [1,2,3,4,5,6,7,8,9,10] 67 for i in 0 ..< numbers.count { 68 print("Count is: \(i)") 69 } 70 71 72 73 74 // for-in遍歷一個數組所有元素: 75 let names = ["Anna","Alex","Brian","Jack"] 76 for name in names { 77 print("Hello, \(name)!") 78 } 79 /** 80 運行結果: 81 Hello,Anna! 82 Hello,Alex! 83 Hello,Brian! 84 Hello,Jack! 85 */ 86 87 88 // 你也可以通過遍歷一個字典來訪問它的鍵值對(key-value pairs)。遍歷字典時,字典的每項元素會以(key, value)元組的形式返回,你可以在for-in循環中使用顯式的常量名稱來解讀(key, value)元組。下面的例子中,字典的鍵(key)解讀為常量animalName,字典的值會被解讀為常量legCount: 89 let numberOfLegs = ["spider":8,"ant":6,"cat":4] 90 for(animalName,legCount)in numberOfLegs { 91 print("\(animalName)s have \(legCount) legs") 92 } 93 /** 94 運行結果 95 spiders have 8legs 96 ants have 6legs 97 cats have 4legs 98 */ 99 100 101 102 // 三、while 用法 103 // 格式:while 布爾值 {} 104 // 說明:只有當while后面的布爾值為false,才停止while語句,否則一直執行while語句 105 var i = 0 //只有滿足條件(i<5)才跳出while語句 106 while (i<5) { 107 print("i=\(i)") 108 i += 1 109 } 110 /** 111 運行結果 112 i=0 113 i=1 114 i=2 115 i=3 116 i=4 117 */ 118 119 120 121 // 四、do {} while 布爾值 do換了個寫法,換成了repeat關鍵字 122 // 說明: 1.現在執行 do 語句 123 // 2.然后再執行while語句 124 // 3.如果while語句后面的布爾值為false就停止do while語句,否則就一直執行do while語句 125 // do while循環沒有初始化語句,循環次數是不可知的,不管循環條件是否滿足,都會先執行一次循環體,然后再判斷循環條件。如果條件滿足則執行循環體,不滿足則停止循環。 126 127 var k=1 128 repeat { 129 130 print("k=\(k)") 131 k += 1 132 133 } while (k<=5) 134 // 135 /** 136 運行結果: 137 i=1 138 i=2 139 i=3 140 i=4 141 i=5 142 */ 143 144 145 // switch 語句 146 // Swift里的switch循環語句里面可以省略了break; 147 148 // 例子 149 let l=1 150 switch (l) { 151 case 0: // case后面必須要跟一個變量,當i=0就執行case當前語句下面對應的方法 152 print("l=\(l)") 153 154 case 1,2: // case后面可以跟兩個變量,多個變量可以用逗號隔開,當i=1或者2的時候就執行case語句下面對應的方法 155 print("l==\(l)") 156 157 default: // 如果i都不滿足一下條件的時候就執行default語句下面對應的方法 158 print("default") 159 160 } 161 /** 162 運行結果: 163 i==1 164 */ 165 166 167 168 // switch 序列匹配 169 /** 170 第一種用法:范圍匹配 171 */ 172 let a=75 173 switch (a) { 174 case 1...50://case后面跟一個序列,序列是一個集合變量,當i在1到50的范圍下就執行case下面對應的方法或語句 175 print("1...50->a=\(a)") 176 case 50...100://case后面跟一個序列,序列是一個集合變量,當i在50到100的范圍下就執行case下面對應的方法或語句 177 print("50...100->a=\(a)") 178 default://如果i的取值范圍都不滿足條件,就執行default下面對應的方法或者語句 179 print("default") 180 } 181 /** 182 運行結果: 183 50...100->i=75 184 */ 185 186 187 188 /** 189 第二種用法:元組匹配 190 */ 191 192 let string = (1,2) // str 是元組變量 193 switch (string) { 194 case (0...1,0...1):// 如果元組變量str的范圍(0到1,0到1) 195 print("(0...1,0...1)-->str=\(str)") 196 case (1...2,1...2):// 如果元組變量str的范圍(1到2,1到2) 197 print("(1...2,1...2)-->str=\(str)") 198 default: 199 print("default") 200 } 201 /** 202 運行結果: 203 (1...2,1...2)-->str=(1, 2) 204 */ 205 206 207 208 } 209 override func didReceiveMemoryWarning() { 210 super.didReceiveMemoryWarning() 211 // Dispose of any resources that can be recreated. 212 } 213 214 215 }