Swift 控制流

For - in 循环

import UIKit

// For - in 循环
// 闭区间
for index in 1...5 {
    print("\(index) times 5 is \(index * 5)")
}

// 数组
var namesArray = ["Jordan", "Kobe", "Wade"]
for name in namesArray {
    print("Hello, \(name)!")
}

// 字典
var numberOfLegs = ["spider" : 8, "ant" : 6, "cat" : 4]
for (animalName, legCount) in numberOfLegs {
    print("\(animalName)s have \(legCount) legs")
}

console log 如下


For-in循环.png

while 循环

// while 循环 (先判断条件,在执行语句)
var i = 0
while i < 10 {
    i += 1
    print("While 第\(i)次循环")
}

console log 如下


while循环.png

repeat - while 循环

// repeat - while 循环 (先执行语句,在判断条件)
var j = 11
repeat {
    j += 1
    print("repeat While 第\(j)次循环")
} while j < 10

console log 如下


repeat-while循环.png
if 条件语句
// if 条件语句
var temperaturInFahrenheit = 40
if temperaturInFahrenheit < 32 {
    print("It's very cold. Consider wearing a scarf.")
} else {
    print("It's not that cold. Wear a t-shirt")
}

console log 如下


if条件语句.png

switch 常规用法(case 分支里面必须有语句,默认只执行一个分支,不用写break)

// switch 常规用法
var someCharacter : Character = "a"
switch someCharacter {
case "a", "A" :
    print("The letter a")
case "b" :
    print("The letter b")
default :
    print("The other letter")
}

console log 如下


switch常规用法.png

switch 区间匹配

// switch 区间匹配
let approximateCount = 62
let countedThings = "moons orbiting Saturn."
var naturalCount : String
switch approximateCount {
case 0:
    naturalCount = "no"
case 1..<5:
    naturalCount = "a few"
case 5..<12:
    naturalCount = "several"
case 12..<100:
    naturalCount = "dozens of"
case 100..<1000:
    naturalCount = "hundreds of"
default:
    naturalCount = "many"
}

print("There are \(naturalCount) \(countedThings)")

console log 如下


switch区间匹配.png

switch 元组匹配

// switch 元组匹配
var somePoint = (1, 1)

switch somePoint {
case (0, 0):
    print("(0, 0) is at the origin")
case (_, 0):
    print("(\(somePoint.0), 0) is at the x-axis")
case (0, _):
    print("(0, \(somePoint.1)) is at 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 the box")
}

console log 如下


switch 元组匹配.png

switch 元组值绑定

// switch 元组值绑定
var otherPoint = (1, 5)
switch otherPoint {
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 an y value of \(y)")
case (let x, let y):
    print("somewhere else at (\(x), \(y))")
}

console log 如下


switch 元组值绑定.png

switch 元组值绑定where 筛选

// switch 元组值绑定where 筛选
var 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 line x == -y")
case let (x, y):
    print("(\(x), \(y)) is just some arbitrary point")
}

console log 如下


switch 元组值绑定where 筛选.png

continue 关键字

// continue 关键字
let puzzleInput = "great minds think alike"
var puzzleOutput = ""
for character in puzzleInput.characters {
    switch character {
        case "a", "e", "i", "o", "u", " ":
        continue
    default :
        puzzleOutput.append(character)
    }
}

print(puzzleOutput)

console log 如下


continue 关键字.png

fallthrough 关键字

// 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
default:
    description += " an integer."
}
print(description)

console log 如下


fallthrough 关键字.png
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Swift提供了多种控制流声明。包括while循环来多次执行一个任务;if,guard和switch声明来根据确定...
    BoomLee阅读 6,036评论 0 3
  • Swift 提供了类似 C 语言的流程控制结构,包括可以多次执行任务的for和while循环,基于特定条件选择执行...
    穷人家的孩纸阅读 3,996评论 1 1
  • Swift提供了各种控制流语句。 这些包括while循环来执行多次任务; if,guard和switch语句,以根...
    Joker_King阅读 3,077评论 0 0
  • Swift提供了多种流程控制结构,包括可以多次执行任务的while循环,基于特定条件基于特定条件选择执行不同代码分...
    edison0428阅读 5,043评论 0 0
  • 一、for循环 for-in循环,遍历集合里面的每个元素: 当不需要区间序列内的值,可以用_来忽略对值的访问: 遍...
    EndEvent阅读 3,073评论 0 2

友情链接更多精彩内容