1...3
代表的是从 1 到 3的整数范围,回圈内的程序会执行 3 次item
代表每次的值,可以自己定义名称for item in 1...3 {
print(item)
}
印出结果:
1
2
3
1..<3
for item in 1..<3 {
print(item)
}
印出结果:
1
2
_
for _ in 1..<3 {
print("hello")
}
印出结果:
hello
hello
where
使用,加上想要的条件,让程序更简洁for item in 1...10 where item%2 == 0 {
print(item)
}
印出结果:
2
4
6
8
10
()
if ["hiking", "ski", "diving"].isEmpty {
print("Empty")
} else {
print("Colorful life!")
}
印出结果:
Colorful life!
else if
let number = 75
if number < 60 {
print("fail")
} else if number >= 60 && number < 85 {
print("good")
} else {
print("excellent")
}
印出结果:
good
break
,只要进到某个条件之後,switch 就不会再跑进别的条件中。let name = "Kevin"
switch name {
case "Yoyo":
print("female")
case "Kevin":
print("male")
default:
print("unknown")
}
印出结果:
male
let number = 75
switch number {
case 0..<60:
print("fail")
case 60..<85:
print("good")
default:
print("excellent")
}
印出结果:
good
where
带入条件类型的 caselet fruit = ["apple", "orange", "banana"]
switch fruit {
case _ where fruit.contains("apple"):
print("This is my favorite fruit.")
case _ where fruit.contains("pineapple"):
print("Not bad.")
default:
print("I don't like these.")
}
印出结果:
This is my favorite fruit.
我们可以自订方法 (function),来做某项特定的事情。
func
func doMath(num: Int) {
print(num * 2)
}
doMath(num: 4)
印出结果:
8
->
func doMath(num1: Int, num2: Int) -> Int {
return num1 + num2
}
print(doMath(num1: 4, num2: 3))
印出结果:
7
func doMath(num1: Int, num2: Int = 5) -> Int {
return num1 + num2
}
print(doMath(num1: 4))
印出结果:
9
今日小结:
今天认识了一些流程控制的方式,以及如何宣告一个方法
明日继续努力!
>>: Day13 Lab 2 - Object storage API层
随着互联网的日新月异,人们对於服务器租用要求也是越来越高,其中高防CDN就是受到大家关注的一种服务类...
曾经跟在便利商店工作过的学弟聊天,从他分享的经验谈,谈到加盟便利超商连锁店,并非每一家都像加盟广告那...
Spark 支援批次资料、查询分析、资料流、机器学习及图处理(Graph Processing),...
近期面试掀起了一波考演算法的风气,就好像回到大学指考那样,老师说这题会考一定要记起来,因此掀起了一...
建立bot 官方文件有说有以下建立方式至於差别在哪呢? # Returns a User insta...