Swift_控制流


点击查看源码

for-in 循环

//for-in 循环
fileprivate func testForIn() {
//直接循环提取内部数据
//[1,5]
for index in 1...5 {
print("\(index) times 5 is \(index * 5)")
} //不需要提取数据 只需要循环次数
let base = 2
let power = 3
var answer = 1
for _ in 1...power {
answer *= base
}
print("\(base) to the power of \(power) is \(answer)") //array遍历
let array = ["XuBaoAiChiYu", "1045214799"]
for item in array {
print("array:\(item)!")
} //Dictionary遍历
let dict = ["name":"XuBaoAiChiYu", "QQ":"1045214799"]
for (key, value) in dict {
print("key:\(key);value:\(value)")
} /* print 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
2 to the power of 3 is 8
array:XuBaoAiChiYu!
array:1045214799!
key:name;value:XuBaoAiChiYu
key:QQ;value:1045214799 */
}

while循环

//while循环
fileprivate func testWhile() {
//先执行while条件判断 后执行内部代码
let count = 4
var index = 0
while index < count {
index += 1
print("while:\(index)")
} /* print while:1
while:2
while:3
while:4 */
}

repeat-while循环

//repeat-while循环
fileprivate func testRepeatWhile() {
//执行内部代码后判断条件
let count = 4
var index = 0
repeat {
index += 1
print("RepeatWhile:\(index)")
} while index < count /* print RepeatWhile:1
RepeatWhile:2
RepeatWhile:3
RepeatWhile:4 */
}

if 判断

//if 判断
fileprivate func testIf() {
//一个条件一个条件的判断 当条件为真时 执行内部程序
let temp = 90
if temp <= 32 {
//不执行
} else if temp >= 86 {
print("执行")
} else {
//不执行
} /* print 执行 */
}

swich判断

//swich判断
fileprivate func testSwitch() {
//基本switch (case不会穿透) let someCharacter: Character = "a"
switch someCharacter {
case "a":
print("1")
case "a", "b", "c":
print("2") //这里不会输出 case找到后 执行完毕就返回(如果需要穿透 加 fallthrough)
default:
print("未找到")
} /* print 1 */
}

switch范围选择

//switch范围选择
fileprivate func testSwitchIntervalMatching() {
//范围选择
let approximateCount = 2
switch approximateCount {
case 1..<5:
print("[1, 5)")
case 5..<10:
print("[5, 10)")
default:
print("未找到")
} /* print [1, 5) */
}

switch元组选择

//switch元组选择
fileprivate func testSwitchTuples() {
//元组选择 坐标轴测试
let random = arc4random()//随机数
let somePoint = (Int(random%3), Int(random%3))//随机数获取点
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")
} /* print (2, 2) is inside the box */
}

switch值选择

//switch值选择
fileprivate func testSwitchValueBindings() {
let random = arc4random()//随机数
//值绑定 如果设置未知数 当匹配成功时 执行此代码
let anotherPoint = (Int(random%3), Int(random%1))
switch anotherPoint {
case (let x, 0):
print("\(x),x匹配")
case (0, let y):
print("\(y),y匹配")
case let (x, y):
print("(\(x), \(y)),其他")
} /* print 1,x匹配 */
}

switch值绑定和where

//switch值绑定和where
fileprivate func testSwitchValueBindingsWhere() {
//使用where条件二次判断
let random = arc4random()//随机数
let yetAnotherPoint = (Int(random%3), Int(random%3))
switch yetAnotherPoint {
case let (x, y) where x < y:
print("\(x) < \(y)")
case let (x, y) where x > y:
print("\(x) > \(y)")
case let (x, y):
print("\(x) = \(y)")
} /* print 1 = 1 */
}

continue

//continue
fileprivate func testContinue() {
//跳过本次循环 继续执行下次循环
for index in 1...5 {
if index == 2 {
continue
}
print("continue:\(index)")
} /* print continue:1
continue:3
continue:4
continue:5 */
}

break

//break
fileprivate func testBreak() { // 跳过当前for或者switch 继续执行
for x in 1...5 {
if x == 2 {
break
}
print("break-x:\(x)")
}
print("break-for") let z = 0
switch z {
case 0:
break;
default:
break;
}
print("break-switch") /* print break-x:1
break-for
break-switch */
}

fallthrough

//fallthrough
fileprivate func testFallthrough() {
//击穿:执行当前case内的代码 并执行下一个case内的代码
let x = Int(arc4random()%1)//0
switch x {
case 0:
print("0")
fallthrough
case 1:
print("1")
fallthrough
default:
break;
} /* print 0
1 */
}

标签

//标签
fileprivate func testLabeledStatements() {
//标签语句 可以直接跳到写标签行的代码
var b = false
go : while true {
print("go")
switch b {
case true:
print("true")
break go //跳过go标签的代码
case false:
print("false")
b = true
continue go //继续执行go标签的代码
}
} /* print go
false
go
true */
}

提前退出

//提前退出
fileprivate func testEarlyExit() {
//guard和if很像 当条件判断为假时 才执行else中的代码
func greet(_ person: [String: String]) {
guard let name = person["name"] else {
print("no name")
return
}
print("name: \(name)!")
} greet([:])
greet(["name": "XU"]) /* print no name
name: XU! */
}

检查API可用性

//检查API可用性
fileprivate func testCheckingAPIAvailability() {
if #available(iOS 9.1, OSX 10.10, *) {
print("iOS 9.1, OSX 10.10, *")
} else {
print("低版本")
} /* print iOS 9.1, OSX 10.10, * */
}

Swift_控制流的更多相关文章

  1. C# 本质论 第三章 操作符和控制流

    操作符通常分为3大类:一元操作符(正.负).二元操作符(加.减.乘.除.取余)和三元操作符( condition?consequence:alternative(consequence和alterna ...

  2. Swift3.0P1 语法指南——控制流

    原档:https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programmi ...

  3. linux 几个控制流语句的格式例子(if语句)

    linux 几个控制流语句的格式例子:if 语句例子:#!/bin/sh a=10b=20 if [ $a == $b ]then echo "a is equal to b"el ...

  4. CSAPP学习笔记(异常控制流1)

    1:诸如子进程结束之后父进程需要被告知,有时候应用程序需要系统调用,内核通过上下文切换将控制从一个进程切换到另一个进程,还有一个进程发送信号到另一个进程时接收者转而到它的信号处理函数去执行等等,我们的 ...

  5. KnockoutJS 3.X API 第四章 数据绑定(2) 控制流foreach绑定

    foreach绑定 foreach绑定主要用于循环展示监控数组属性中的每一个元素,一般用于table标签中 假设你有一个监控属性数组,每当您添加,删除或重新排序数组项时,绑定将有效地更新UI的DOM- ...

  6. java基础-控制流语句

    浏览以下内容前,请点击并阅读 声明 一般情况下,代码的执行按照从上到下的顺序,然而通过加入一些判断,循环和跳转语句,你可以有条件地执行特定的语句. 接下来分三部分介绍Java的控制流语句,他们是判断语 ...

  7. 简明python教程 --C++程序员的视角(一):数值类型、字符串、运算符和控制流

    最初的步骤 Python是大小写敏感的 任何在#符号右面的内容都是注释 >>> help('print')在“print”上使用引号,那样Python就可以理解我是希望获取关于“pr ...

  8. python学习笔记系列----(二)控制流

    实际开始看这一章节的时候,觉得都不想看了,因为每种语言都会有控制流,感觉好像我不看就会了似的.快速预览的时候,发现了原来还包含了对函数定义的一些描述,重点讲了3种函数形参的定义方法,章节的最后讲述了P ...

  9. CPS冥想 - 2 手撸控制流

    原博客链接:http://blogs.msdn.com/b/ericlippert/archive/2010/10/22/continuation-passing-style-revisited-pa ...

随机推荐

  1. 关于GBK、GB2312、UTF8之间的区别

    UTF-8:Unicode Transformation Format-8bit,允许含BOM,但通常不含BOM.是用以解决国际上字符的一种多字节编码,它对英文使用8位(即一个字节),中文使用24为( ...

  2. guava的限流工具RateLimiter使用

    guava限流工具使用 非常详细的一篇使用博客:https://www.cnblogs.com/yeyinfu/p/7316972.html 1,原理:Guava RateLimiter基于令牌桶算法 ...

  3. 移除script标签引起的兼容性问题

    一.应用场景: 有时候我们需要动态创建script标签实现脚本的按需加载,我们会为script标签绑定onload或者onreadystatechange事件,用于检测动态脚本是否加载并执行完毕,在事 ...

  4. Java集合篇六:Map中key值不可重复的测试

    package com.test.collection; import java.util.HashMap; import java.util.Map; //Map中key值不可重复的测试 publi ...

  5. scss-!default默认变量

    在变量赋值之前, 利用!default为变量指定默认值. 也就是说,如果在此之前变量已经赋值,那就不使用默认值,如果没有赋值,则使用默认值. 代码实例如下: $content: "antzo ...

  6. 提取url中参数的方法(转换成json格式)

    还是直接上代码吧. //将url中的参数获取到并抓换成json格式 function serilizeUrl(url){ var urlObject={}; //1.正则匹配是不是以?结尾 if(/\ ...

  7. SharePoint Designer - View

    1. 数据视图 可以将图片.新闻等列表(如: Announcement)用以下视图显示,具体做法可以参考这篇文章,但需要强调几个地方: 1.1 选择了视图样式后,需要点击“自定义” --> &q ...

  8. std::string, std::wstring, wchar_t*, Platform::String^ 之间的相互转换

    最近做WinRT的项目,涉及到Platform::String^  和 std::string之间的转换,总结一下: (1)先给出源代码: std::wstring stows(std::string ...

  9. maven学习(一)setting.xml配置文件详解

    maven环境搭建: 1.官网下载zip包,解压至任意目录(如:E:\wly\apache-maven-3.2.5) 2.环境变量MAVEN_HOME(E:\wly\apache-maven-3.2. ...

  10. SqlServer存储过程示例

    /* 步骤1 删除本地及海关单证待分派表.报关单表中的数据 delete from W_DOCUMENTS; delete from W_DOCUMENTS_TEST; delete from W_D ...