您好,登录后才能下订单哦!
条件控制: if 和 Switch
循环控制: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 循环,不再限定类型:
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循环如下:
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;
}
下一篇文章将讲解方法、函数
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。