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. LINQ和.NET数据访问

    .NET数据访问 在.NET中对于数据的访问大致有三个层面,数据访问层.内存数据集.业务逻辑层.数据层,包括了XML配置文件以及一些常用的数据库(使用SQL语句):内存数据集,主要是DataSet数据 ...

  2. solidity语言

    IDE:Atom 插件:autocomplete-solidity 代码自动补齐   linter-solium,linter-solidity代码检查错误   language-ethereum支持 ...

  3. linkedHashMap源码解析(JDK1.8)

    引言 关于java中的不常见模块,让我一下子想我也想不出来,所以我希望以后每次遇到的时候我就加一篇.上次有人建议我写全所有常用的Map,所以我研究了一晚上LinkedHashMap,把自己感悟到的解释 ...

  4. node:fs-extra模块

    var fs = require('fs-extra'); //复制 并会覆盖已有文件 fs.copy('./demo/index.html','./demo/index2.html' ,(err) ...

  5. ppt写作的注意事项

    PPT推荐字体及大小: 宋体严谨,适合正文,显示最清晰 黑体庄重,适合标题,或者强调区 隶书楷体,艺术性强,不适合投影 如果通过文字排版突出重点:加粗.加大字号.变色 PPT文字太多怎么办? 1.抽象 ...

  6. 在Eclipse安装Genymotion插件的经验心得

    个人心得分享,不当之处还请指正. Eclipse自带的Android模拟器已经无力吐槽了,新手刚上手时或许配置完环境已经精疲力尽了,或许还沉浸在开发成功的喜悦当中,对AVD模拟器的运行情况关注不大,渐 ...

  7. jetbrain rider 逐渐完美了,微软要哭了么?

    2019-03-24 10:08:42 多年的vsiual studio使用经验,各种小瑕疵:到现在的visual studio是越来越大了:简直到了无法忍受境地: 每次重装系统都要重新安装下,这个不 ...

  8. AWS的登录认证。。。

    Hello, I’m sorry for any concern regarding the $1.00 Authorization that you see associated with your ...

  9. 在centos7中安装redis,并通过node.js操作redis

    引言 最近在学习node.js 连接redis的模块,所以尝试了一下在虚拟机中安装cent OS7,并安装redis,并使用node.js 操作redis.所以顺便做个笔记. 如有不对的地方,欢迎大家 ...

  10. libxml2库函数详解

    许多事物符合80/20法则,libxml中也是20%的函数提供了80%的功能.下面的列表列出了libxml的主要函数及其用法说明. 1.   全局函数说明 头文件引用 xml2config --cfl ...