條件控制: if 和 Switch
在欒城等地區(qū),都構建了全面的區(qū)域性戰(zhàn)略布局,加強發(fā)展的系統性、市場前瞻性、產品創(chuàng)新能力,以專注、極致的服務理念,為客戶提供成都網站設計、成都網站建設 網站設計制作按需求定制開發(fā),公司網站建設,企業(yè)網站建設,成都品牌網站建設,全網整合營銷推廣,成都外貿網站制作,欒城網站建設費用合理。
循環(huán)控制:for
-in
, for
, while
, and do
-while
1 參考代碼如下:
let classScore = [100,90,80,70,60,50,40,30]
var totalScore = 0
for scoreinclassScore{
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 = ""
switchvegetable{
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;
}
下一篇文章將講解方法、函數