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_316_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

相关文章

软件简介:蓝湖辅助工具,减少移动端开发中控件属性的复制和粘...
现实生活中,我们听到的声音都是时间连续的,我们称为这种信...
前言最近在B站上看到一个漂亮的仙女姐姐跳舞视频,循环看了亿...
【Android App】实战项目之仿抖音的短视频分享App(附源码和...
前言这一篇博客应该是我花时间最多的一次了,从2022年1月底至...
因为我既对接过session、cookie,也对接过JWT,今年因为工作...