1,if语句
2,switch语句
5
(2)case分支还可以进行区间匹配
9
(3)使用元组匹配(判断属于哪个象限)
10
(4)case中还可以使用where关键字来做额外的判断条件
var
3,for循环语句
3
(2)for-in循环
13
4,while循环语句
7
1
2
3
4
5
|
if
count >=3 {
print
(
"yes"
)
}
else
{
"no"
)
}
|
(1)Swift中不需要在case块中显示地使用break跳出switch。如果想要实现C风格的落入特性,可以给需要的case分支插入fallthrough语句
let
fruit =
"apple"
switch
fruit{
case
"apple"
:
"good"
fallthrough
"banana"
,
"orange"
:
"great"
)
default
:
"bad"
)
}
|
age = 5
age {
case
0...11:
"正太"
12...30:
"少年"
)
:
"大叔"
)
}
|
point = (2,2)
point {
(0,0):
"坐标在原点"
(_,0):
"坐标在x轴上"
"坐标在y轴上"
(-3...3,-3...3):
"坐标在长宽为6的正方形内"
)
:
"在什么地方"
)
}
|
(1)for条件递增循环
for
i=1; i<100; i++ {
@H_533_301@"\(i)"
}
|
for
i
in
1..<100{
}
//遍历数组元素
numbers = [1,2,4,7]
num
numbers{
"\(num)"
}
//遍历字典
nameOfAge = [
"lily"
:18,monospace!important; min-height:inherit!important; color:blue!important">"Candy"
:24]
(aName,iAge)
nameOfAge{
"\(aName) is \(iAge)"
)
}
//遍历字符串的字符
chare
@H_127_404@(chare)
in
"hangge"
.characters {
}
|
while
i<100 {
i++
repeat{
i++
i<100
|
原文出自: www.hangge.com 转载请保留原文链接: http://www.hangge.com/blog/cache/detail_516.html 原文链接:https://www.f2er.com/swift/324953.html