標籤:while語句 res rip 字典 論壇 ant tin time for 迴圈
分支語句
1. 條件陳述式 if-else ,基本的文法形式例如以下。當中 “[]” 中 的部分能夠省略。
if boolean-expression { statement1; } [else if boolean-expression { statement2; } ] [else {statement3; } ]
執行個體:
var number1 = 0;var number2 = 1; var max = 0; if number1 >number2 { max = number1; } else if number1 < number2 { max = number2; } else { max = number1; }println("Themaximum is \(max)")
2 .多分支語句 switch 它的文法形式例如以下:
switch some value to consider { case value 1: respond to value 1 case value 2, value 3:respond to value 2 or 3default: otherwise, do something else }
每一個 case 不須要顯式地加入 break ,每一個 case 至少有 一條語句。
能夠比較不論什麼類型。
執行個體1:
let someCharacter: Character ="e" switch someCharacter {case "a", "e", "i","o", "u": println("\(someCharacter) isa vowel")case "b", "c", "d", "f", "g", "h", "j", "k", "l","m", "n", "p", "q", "r", "s", "t", "v", "w", "x", "y", "z":println("\(someCharacter) is a consonant") default: println("\(someCharacter) is not a vowel or a consonant") }
執行個體2:
let count = 3_000 let countedThings = "stars inthe Milky Way"varnaturalCount: Stringswitch count { case 0: naturalCount ="no" case 1...3:naturalCount ="a few" case 4...9:naturalCount ="several" case 10...99: naturalCount ="tens of" case 100...999:naturalCount ="hundreds of" case 1000...999_999:naturalCount ="thousands of" default:naturalCount = "millions and millions of" } println("There are \(naturalCount) \(countedThings).")
迴圈語句
while語句
do-while語句
for語句
for in語句
while 迴圈
while condition{ statements }vari = 100 varr = 0 vars= 0 vart = 0while i < 1000 {r= i / 100 s=( i - r* 100 ) / 10 t= i - r* 100 - s* 10 if( i == r * r* r + s* s *s + t* t * t){ println ( "i = \(i)" ) }i++}do while 迴圈 do { statements } while conditionvari = 100 varr = 0 vars= 0 vart = 0do { r= i / 100 s=( i - r* 100 ) / 10 t= i - r* 100 - s* 10 if( i == r * r* r + s* s *s + t* t * t){ println ("i = \(i)" ) } i++} while i < 1000
for 迴圈
for initialization; condition; increment {statements}vari= 8 varr = 0 vars= 0forvarj =0; j <=i; j++{ r = j * j s=j * j * jprintln (" 整數為 :\(j) "); println (" 相應的平方和 :\( r)") println ( " 相應的立方和 : \( s ) " ) }
for in 迴圈
用於遍曆集合。
1. 遍曆範圍
forindex in1...5{ println( " \(index) times 5 is \(index * 5 )" ) }
2. 忽略迴圈變數
letbase = 3letpower = 10 varanswer = 1for_ in 1...power { answer *=base } println (" \(base )to the power of \(power )is \(answer ) " )
3. 遍曆數組
letnames =[ "Anna" ,"Alex" , "Brian" ,"Jack"] forname innames { println( "Hello, \(name) !" ) }
4. 遍曆字典
letnumberOfLegs =[ "spider" :8 , "ant" :6 , "cat" : 4] for(animalName, legCount) innumberOfLegs { println( " \(animalName) s have \(legCount) legs" ) }
5 .遍曆字串
letnumberOfLegs =[ "spider" :8 , "ant" :6 , "cat" : 4] for(animalName, legCount) innumberOfLegs { println( " \(animalName) s have \(legCount) legs" ) } letnumberOfLegs =[ "spider" :8 , "ant" :6 , "cat" : 4] for(animalName, legCount) innumberOfLegs { println( " \(animalName) s have \(legCount) legs" ) }
1.12.3 跳躍陳述式
? continue
? break
? fallthrough
? return
1.continue 執行個體:
for var i = 0; i < 100; i++ { if i % 10 == 0 { continue; } println("i = \(i)"); }
2. break 執行個體:
for var i = 0; i < 10;i++ { if i == 3 { break; }println("i = \(i)");} println("Game Over!");
3. fallthrough 執行個體:
let integerToDescribe = 5 var description = "Thenumber \(integerToDescribe) is" switchintegerToDescribe { case 2, 3, 5, 7, 11, 13, 17, 19: description += " a prime number, and also"fallthroughdefault: description += " an integer."} println(description)
Swift交流討論論壇論壇:http://www.cocoagame.net
歡迎加入Swift技術交流群:362298485
Swift控制語句