3.循环语句
for
let individualScores = [75, 43, 103, 87, 12]
for score in individualScores {
if score > 50 {
print("\(score)")
}
}
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
}
}
}
print(largest)
> Prints "25"
while
var n = 2
while n < 100 {
n *= 2
}
print(n)
repeat while
var m = 2
repeat {
m *= 2
} while m < 100
print(m)
for in ...
var total = 0
//注意 ... 包含左右 ..< 包左不包右
for i in 0..<4 {
total += i
}
print(total)
网友评论