Swift2.2 学习笔记(十二) ___控制流
For-In
for index in 1...5 {//闭区间操作符(...)print("\(index) times 5 is \(index * 5)")//注意index 常量只存在于循环的生命周期里。
}
/*
1 times 5 is 5
2 times 5 is 10
3 times 5 is 15
4 times 5 is 20
5 times 5 is 25
*/
如果不需要知道范围内的每一项的值,可以使用下划线(_)替代变量名来忽略对值的访问
let base = 3
let power = 10
var answer = 1
for _ in 1...power {answer *= base // 3的十次幂
}
print ("\(base) to the power of \(power) is\(answer)")//3 to the power of 10 is59049
使用for-in遍历一个数组所有元素
let names = ["Anna","Alex","Brian","Jack"]
for name in names {print("Hello,\(name)!")
}let numberOfLegs = ["spider":8,"ant":6,"cat":4]
for (animalName,legCount) in numberOfLegs {print("\(animalName)s have \(legCount) legs")
}
let numberOfLegs = ["spider": 8, "ant": 6, "cat": 4]
for (animalName, legCount) in numberOfLegs {print("\(animalName)s have \(legCount) legs")
}
// ants have 6 legs
// cats have 4 legs
// spiders have 8 legs
For条件递增 (for-condition-increment)
for var index = 0; index < 3; ++index {print("index is \(index)")
}var index:Int
for index = 0; index < 3; ++index {print("index is \(index)")
}
print("The loop statements were executed \(index) times")
while 循环
while condition {
statements
}
let finalSqure = 25
var board = [Int](count: finalSqure + 1, repeatedValue: 0)
board[03] = +08; board[06] = +11; board[09] = +09; board[10] = +02
board[14] = -10; board[19] = -11; board[22] = -02; board[24] = -08var square = 0
var diceRoll = 0
while square < finalSqure {if ++diceRoll == 7 { diceRoll = 1 }square += diceRollif square < board.count {square += board[square]}
}
print("GameOver!")
Do-While
do {
statements
} while condition
//条件语句
//If
var temperatureInFahrenheit = 30
if temperatureInFahrenheit <= 32 {print("it's very cold. Consider wearing a scarf")
}// Switch
let somCharacter:Character = "e"
switch somCharacter {case "a","b":print("1")case "c","d","e":print("2")default:print("nothing")
}
元祖,在同一个switch语句中测试多个值。元组中的元素可以是值,也可以是范围。使用(_)来匹配所有可能的值
let somePoint = (1,1)
switch somePoint {case (0,0):print("(0,0) is at the origin")case (_,0):print("(\(somePoint.0), 0) is on the x-axis")case (0,_):print("(0, \(somePoint.1)) is on the y-axis")case (-2...2,-2...2):print("(\(somePoint.0),\(somePoint.1)) is inside the box")default:print("(\(somePoint.0),\(somePoint.1) is outside of the box")// (1,1) is inside the box}
值绑定
case块的模式允许将匹配的值绑定到一个临时的常量或者变量,这些常量或变量在该case块里就可以被引用了
let anotherPoint = (2,0)
switch anotherPoint {
case (let x,0):print("on the x-axis with an x value of \(x)")
case (0, let y):print("on the y-axis with a y value of \(y)")
case let (x,y):print("somewhee else at (\(x),\(y))")// on the x-axis with an x value of 2
}
Where
case块的模式可以使用where语句来判断额外的条件
let yetAnotherPoint = (1,-1)
switch yetAnotherPoint {
case let (x, y) where x == y:print("(\(x),\(y)) is on the line x == y")
case let (x,y) where x == -y:print("(\(x),\(y) is on the linx x == -y)")
case let (x,y):print("(\(x),\(y) is just some arbirthary point)")// (1,-1 is on the linx x == -y)}
Swift四种控制转移语句
-continue
-break
-fallthrough
-return
//continue 语句告诉循环停止正在做的事情并且再次从开始循环的下一次遍历。就是说“我不再继续当前的循环遍历了”而不是离开整个的循环
let puzzleInput = "great minds think alike"
var puzzleOutput = ""
for character in puzzleInput.characters {switch character {case "a" ,"e","i","o","u":continuedefault:puzzleOutput.append(character)}
}
print(puzzleOutput) //grt mnds thnk lk
Break 立即结束整个控制流语句。
当在switch语句里使用时,break 导致 switch 语句立即结束它的执行,并且转移控制到 switch 语句结束花括号( })之后的第一行代码上
let numberSymbol: Character = "三"
var possibleIntegerValue:Int?
switch numberSymbol {
case "1", "١", "一", "๑":possibleIntegerValue = 1
case "2", "٢", "二", "๒":possibleIntegerValue = 2
case "3", "٣", "三", "๓":possibleIntegerValue = 3
case "4", "٤", "四", "๔":possibleIntegerValue = 4
default:break
}
if let integerValue = possibleIntegerValue {print("The integer value of \(numberSymbol) is \(integerValue).")
} else {print("An integer value could not be found for \(numberSymbol).")
}
//The integer value of 三 is 3.
Fallthrough
如果你确实需要 C 风格的贯穿行为,你可以选择在每个情况末尾使用 fallthrough 关键字。
let integerToDescribe = 5
var description = "The number \(integerToDescribe) is "
switch integerToDescribe {
case 2,3,5,7,11,13,17,19:description += "a prime number ,and also"fallthrough
// 使用 fallthrough 关键字来“贯穿到” default 情况。 default 情况添加额外的文字到描述的末尾,接着 switch 语句结束。
default:description += "an integer."
}
print(description)
Swift2.2 学习笔记(十二) ___控制流相关推荐
- 吴恩达《机器学习》学习笔记十二——机器学习系统
吴恩达<机器学习>学习笔记十二--机器学习系统 一.设计机器学习系统的思想 1.快速实现+绘制学习曲线--寻找重点优化的方向 2.误差分析 3.数值估计 二.偏斜类问题(类别不均衡) 三. ...
- ROS学习笔记十二:使用roswtf
ROS学习笔记十二:使用roswtf 在使用ROS过程中,roswtf工具可以为我们提供ROS系统是否正常工作的检查作用. 注意:在进行下列操作之前,请确保roscore没有运行. 检查ROS是否安装 ...
- Python语言入门这一篇就够了-学习笔记(十二万字)
Python语言入门这一篇就够了-学习笔记(十二万字) 友情提示:先关注收藏,再查看,12万字保姆级 Python语言从入门到精通教程. 文章目录 Python语言入门这一篇就够了-学习笔记(十二万字 ...
- Polyworks脚本开发学习笔记(十二)-输出和读取文本文件
Polyworks脚本开发学习笔记(十二)-输出和读取文本文件 Polyworks作为一个测量工具,将测量的数据方便的导出到文本文件则是一项必须的功能.在DATA_FILE这个命令下提供了很多子命令用 ...
- OpenCV学习笔记(十二)——图像分割与提取
在图像处理的过程中,经常需要从图像中将前景对象作为目标图像分割或者提取出来.例如,在视频监控中,观测到的是固定背景下的视频内容,而我们对背景本身并无兴趣,感兴趣的是背景中出现的车辆.行人或者其他对象. ...
- 【现代机器人学】学习笔记十二:轮式移动机器人
目录 轮式机器人类型 全向轮式机器人 建模 单个全向轮是怎么运动的 多个全向轮是如何带动底盘运动的 运动规划和反馈控制 非完整约束轮式移动机器人 建模 独轮车 差速驱动机器人 车型机器人 非完整移动机 ...
- 【theano-windows】学习笔记十二——卷积神经网络
前言 按照进度, 学习theano中的卷积操作 国际惯例, 来一波参考网址 Convolutional Neural Networks (LeNet) 卷积神经网络如何应用在彩色图像上? 卷积小知识 ...
- (C/C++学习笔记) 十二. 指针
十二. 指针 ● 基本概念 变量的地址就是指针,存放指针的变量就是指针变量(因而又叫作地址变量 address variable); 这个地址编号本身就是一个无符号的整数,在32位系统下为4字节(8位 ...
- Vue.js 学习笔记 十二 Vue发起Ajax请求
首先需要导入vue-resource.js,可以自己下载引入,也可以通过Nuget下载,它依赖于Vue.js. 全局使用方式: Vue.http.get(url,[options]).then(suc ...
最新文章
- android 拼图课程设计,Flash拼图游戏制作课程设计报告
- Firefox 与 IE 已死?Chrome 一统天下!
- sturts2 单上传、多上传、下载例子
- Paul Rayner认为DDD和敏捷可以共存
- C# 8 新特性 - 只读struct成员
- lower版购物车模拟
- 向前欧拉公式例题_小学语文阅读理解答题万能公式,简单实用!
- java内存模型之二volatile内存语义
- RuntimeError: expected backend CUDA and dtype Float but got backend CUDA and dtype Long
- 6-2 函数式编程例一
- sparkpython多线程_如何在PySpark(Spark流)中组合多个rdd?
- 剪贴画制作相关资源收集
- [软件工程] 可行性研究
- 虚拟机VMware安装ubuntu教程
- (XWZ)的Python学习笔记Ⅱ------面向对象编程
- hazelcast java_hazelcast初探
- 69A.Young Physicist
- 希尔排序选择排序时间复杂度分析
- matlab 求对称,matlab-线性代数 判断 矩阵的对称、实对称、反对称
- 海信A5 pro 测评
热门文章
- c#截取字符串中指定字符串后到结尾
- 穹顶灯打不出阴暗面_微服务的阴暗面,解释
- SuperMap iClient3D for WebGL教程- 淹没分析
- 《密码朋克:自由与互联网的未来》[澳] 朱利安-阿桑奇
- 红帽如何安装oracle数据库,红帽系Linux安装Oracle 19C数据库
- springboot整合flyway出现Correct the classpath of your application so that it contains a single,compatibl
- 点关于任意直线的对称点
- Could not find action or result No result defined for action
- vijos1153:猫狗大战
- 怎么优化自己网站的关键词_新网站seo必做步骤