Swift - 控制流/控制结构说明(if,switch,for,while)
for 说明 swift if while switch 控制流
2023-09-11 14:17:59 时间
1,if语句
2,switch语句
(2)case分支还可以进行区间匹配
(3)使用元组匹配(判断属于哪个象限)
(4)case中还可以使用where关键字来做额外的判断条件
3,for循环语句
(2)for-in循环
4,while循环语句
1
2
3
4
5
|
if count >=3 { println ( "yes" ) } else { println ( "no" ) } |
(1)Swift中不需要在case块中显示地使用break跳出switch。如果想要实现C风格的落入特性,可以给需要的case分支插入fallthrough语句
1
2
3
4
5
6
7
8
9
10
|
var fruit = "apple" switch fruit{ case "apple" : println ( "good" ) fallthrough case "banana" , "orange" : println ( "great" ) default : println ( "bad" ) } |
1
2
3
4
5
6
7
8
9
|
var age = 5 switch age { case 0...11: println ( "正太" ) case 12...30: println ( "少年" ) default : println ( "大叔" ) } |
1
2
3
4
5
6
7
8
9
10
11
12
13
|
let point = (2,2) switch point { case (0,0): println ( "坐标在原点" ) case (_,0): println ( "坐标在x轴上" ) case (0,_): println ( "坐标在y轴上" ) case (-3...3, -3...3): println ( "坐标在长宽为6的正方形内" ) default : println ( "在什么地方" ) } |
1
2
3
4
5
6
7
8
9
|
var height = 1.72 switch height{ case 1...3 where height == 1.72: println ( "case 1" ) case 1...3 where height == 2: println ( "case 2" ) default : println ( "default" ) } |
(1)for条件递增循环
1
2
3
|
for var i=1; i<100; i++ { println ( "\(i)" ) } |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
for i in 1..<100{ println ( "\(i)" ) } //遍历数组元素 let numbers = [1,2,4,7] for num in numbers{ println ( "\(num)" ) } //遍历字典 let nameOfAge = [ "lily" :18, "Candy" :24] for (aName, iAge) in nameOfAge{ println ( "\(aName) is \(iAge)" ) } //遍历字符串的字符 for chare in "hangge" { println (chare) } |
1
2
3
4
5
6
7
|
while i<100 { i++ } do{ i++ } while i<100 |
相关文章
- config : 'option forwardfor' ignored for frontend 'openshift-api-server' as it requires HTTP mode.
- Creating a Unity Game for Windows 8
- jdk8新特性:在用Repository实体查询是总是提示要java.util.Optional, 原 Inferred type 'S' for type parameter 'S' is not within its bound;
- Docker的安装和使用说明——Docker for Windows
- [React Flow] Up and Running with Facebook Flow for Typed JavaScript
- webbench 1.5 for linux 使用说明
- SAP成都研究院C4C光明左使:SAP Cloud for Customer 使用SAP UI5的独特之处
- SAP CRM和Cloud for Customer里的Formatted Text控件的比较
- SAP Cloud for Customer移动端应用SAP推荐的网络时延参数
- TVideoGrabber SDK 15.2.4 add for delphi Crack
- Spire.PDF for Java 8.12.6 supports creating tagged PDF files
- Spire.PDF for Java 8.11.0 Spire.PDF for Java
- 成功解决Building wheel for dlib (setup.py) ... error ERROR: Command errored out with exit status 1:
- 【异常】IDEA中提示Maven报错,Blocked mirror for repositories:[nexus (http://xxxxx, default , releases+snapshot
- Neutron数据库同步错误 NotImplementedError: No support for ALTER of constraints in SQLite dialect
- 3GPP TS 23501-h20 中英文对照 | 5.8.2.13 Support for 5G VN group communication
- 问题解决:Failed to download metadata for repo ‘appstream‘: Cannot prepare internal mirrorlist:...
- 《论文阅读》Multi-Task Learning of Generation and Classification for Emotion-Aware Dialogue Response Gener