微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Swift - 控制流/控制结构说明if,switch,for,while

1,if语句
1
2
3
4
5
if count >=3 {
print ( "yes" )
} else {
"no" )
}

2,switch语句
(1)Swift中不需要在case块中显示地使用break跳出switch。如果想要实现C风格的落入特性,可以给需要的case分支插入fallthrough语句
5
6
7
8
9
10
let fruit = "apple"
switch fruit{
case "apple" :
"good" fallthrough
"banana" , "orange" :
"great" )
default :
"bad" )
}
(2)case分支还可以进行区间匹配
9
age = 5
age {
case 0...11:
"正太" 12...30:
"少年" )
:
"大叔" )
}
(3)使用元组匹配(判断属于哪个象限)
10
11
12
13
point = (2,2)
point {
(0,0):
"坐标在原点" (_,0):
"坐标在x轴上" ottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.5em!important; margin:0px!important; overflow:visible!important; padding:1px 0px!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,_):
"坐标在y轴上" (-3...3,-3...3):
"坐标在长宽为6的正方形内" )
:
"在什么地方" )
}
(4)case中还可以使用where关键字来做额外的判断条件
varheight = 1.72
height{
1...3 where height == 1.72:
"case 1" height == 2:
"case 2" "default" }

3,for循环语句
(1)for条件递增循环
3
for i=1; i<100; i++ {
"\(i)" }
(2)for-in循环
13
14
15
16
17
18
19
20
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 in "hangge" .characters {
@H_243_404@(chare)
}

4,while循环语句
7
while i<100 {
i++
repeat{
i++
i<100

原文出自: www.hangge.com 转载请保留原文链接 http://www.hangge.com/blog/cache/detail_516.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐