您好,登錄后才能下訂單哦!
條件控制: if 和 Switch
循環(huán)控制:for
-in
, for
, while
, and do
-while
1 參考代碼如下:
let classScore = [100,90,80,70,60,50,40,30]
var totalScore = 0
for score in classScore{
if score >= 60 {
totalScore += score
} else {
println("no pass,score is \(score)" );
}
}
println("totalScore is \(totalScore)");
2 switch case 循環(huán),不再限定類型:
let vegetable = "Switch case Type"
var result = ""
switch vegetable{
case "calery":
result = "calery"
case "calendar","china":
result = "china"
case let x where x.hasSuffix("Type"):
result = "haSuffix";
default :
result = "default"
}
println("result :\(result)")
3 使用for-in 遍歷字典:
let interestingNumbers = [
"Prime": [2, 3, 5, 7, 11, 13],
"Fibonacci": [1, 1, 2, 3, 5, 8],
"Square": [1, 4, 9, 16, 25],
]
var largest = 0
for (kind, numbers) in interestingNumbers {
for number in numbers {
if number > largest {
largest = number
}
}
}
println("largest:\(largest)");
4 while 和 do while代碼如下:
var n = 2
while n < 100 {
n = n * 2
}
var m = 2
do {
m = m * 2
} while m < 100
5 for循環(huán)如下:
var firstLooper = 0;
for i in 0..3{
println("firstLooper:\(i)")
firstLooper += 1;
}
var secondLopper = 0;
for var i = 0;i < 3 ; ++i{
println("secondLopper:\(i)")
secondLopper += 1;
}
下一篇文章將講解方法、函數(shù)
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。