美文网首页
Swift 控制流

Swift 控制流

作者: 点滴86 | 来源:发表于2016-08-04 20:01 被阅读4次

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

相关文章

  • 9.控制流(theControlFlow)

    控制流 kotlin_控制流 swift控制流

  • Swift教程之控制流

    控制流 Swift包含很多控制流语句:while循环、if、guard、switch和for-in循环。 For-...

  • Swift5.x-函数(中文文档)

    引言 继续学习Swift文档,从上一章节:控制流,我们学习了Swift控制流相关的内容,如for-in循环、whi...

  • Swift 控制流

  • Swift:控制流

    中文文档 一、For-In 循环 你可以使用 for-in 循环来遍历一个集合中的所有元素 使用 for-in 遍...

  • Swift控制流

    Swift控制流 for 循环 Swift中使用for-in的形式来进行for循环,类似于C语言的for条件递增的...

  • Swift -- 控制流

    控制流(Control Flow) Swift提供了多种流程控制结构,包括可以多次执行任务的while循环,基于特...

  • Swift - 控制流

    控制流语句,用于控制程序正在执行的流程。Swift中,控制流语句主要为条件语句和循环语句。 条件语句 if条件语句...

  • swift——控制流

    Swift 提供了类似 C 语言的流程控制结构,包括可以多次执行任务的for和while循环,基于特定条件选择执行...

  • Swift 控制流

    For - in 循环 console log 如下 while 循环 console log 如下 repeat...

网友评论

      本文标题:Swift 控制流

      本文链接:https://www.haomeiwen.com/subject/egacsttx.html