swift(5)流程控制

for循环分两种:for-in和for-条件-自增型

在for index in 1...5{}中,index作为一个let常量,只能在for内使用,如果希望该值在for外使用,则在for循环前声明var变量

如果只使用次数,不使用值,则可省略变量名,使用下划线代替。for _ in 1...count{}

关于数组和字典的遍历,可使用for-in,或者遍历一个字符串的每个字符

for var index=0;index<3;++index{},与C类似,只是没有括号

while和do while,用法与C类似,但是其中条件语句也是没有小括号。

条件语句if 用法与C类似,其中条件语句也不适用小括号

分支语句switch:

let someCharacter: Character = "e"
switch someCharacter {
    case "a", "e", "i", "o", "u":
        println("\(someCharacter) is a vowel")
    case "b", "c", "d", "f", "g", "h", "j", "k", "l", "m",
"n", "p", "q", "r", "s", "t", "v", "w", "x", "y", "z":
        println("\(someCharacter) is a consonant")
    default:
        println("\(someCharacter) is not a vowel or a consonant")
}

多个条件符合同一分支的,使用逗号分隔,可换行,不存在两个case同时符合的情况,因此不需要使用break关键字(写上也没有错)

每个case  :下必须要可执行语句,否则编译报错。链接:fallthrough关键字

case :条件可使用类似1...1000区间表示数据

let count = 3_000_000_000_000
let countedThings = "stars in the Milky Way"
var naturalCount: String
switch count {
case 0:
    naturalCount = "no"
case 1...3:
    naturalCount = "a few"
case 4...9:
    naturalCount = "several"
case 10...99:
    naturalCount = "tens of"
case 100...999:
    naturalCount = "hundreds of"
case 1000...999_999:
    naturalCount = "thousands of"
default:
    naturalCount = "millions and millions of"
}
println("There are \(naturalCount) \(countedThings).")
// prints "There are millions and millions of stars in the Milky Way."

元组可作为case :条件,元组的一部分可用下划线(_)表示任何值

let somePoint = (1, 1)
switch somePoint {
case (0, 0):
    println("(0, 0) is at the origin")
case (_, 0):
    println("(\(somePoint.0), 0) is on the x-axis")
case (0, _):
    println("(0, \(somePoint.1)) is on the y-axis")
case (-2...2, -2...2):
    println("(\(somePoint.0), \(somePoint.1)) is inside the box")
default:
    println("(\(somePoint.0), \(somePoint.1)) is outside of the box")
}
// prints "(1, 1) is inside the box"

image: ../Art/coordinateGraphSimple_2x.png

上述例子需要说明的是case第一个条件如果符合的话,不再对比以后的case,否则(0,0)点在每一个条件中了。

case中可绑定一个值到一个常量或变量中,例如:

let anotherPoint = (2, 0)
switch anotherPoint {
case (let x, 0):
    println("on the x-axis with an x value of \(x)")
case (0, let y):
    println("on the y-axis with a y value of \(y)")
case let (x, y):
    println("somewhere else at (\(x), \(y))")
}
// prints "on the x-axis with an x value of 2"

case (let x,0):表示y值为0,x为任何值的都符合该case分支,并且x被赋予元组第一个成员的值,可在本case下使用。

上例还需说明一点:最后一个case包含了条件所有值,所以不再需要default分支。如果case并没有包含所有分支,则default必须在最后一个分支出现。

switch case可以使用where作附加条件,在C语言中是没有的,下例是表示坐标中的对斜线

let yetAnotherPoint = (1, -1)
switch yetAnotherPoint {
case let (x, y) where x == y:
    println("(\(x), \(y)) is on the line x == y")
case let (x, y) where x == -y:
    println("(\(x), \(y)) is on the line x == -y")
case let (x, y):
    println("(\(x), \(y)) is just some arbitrary point")
}
// prints "(1, -1) is on the line x == -y"

continue,break与C语言用法相同

fallthrough使用在switch中,如果在一个case中使用了fallthrough,则继续执行下一个分支判断。在C语言中使用break显式指定中止switch,而在swift中使用fallthrough显式指定继续switch判断。

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."
}
println(description)
// prints "The number 5 is a prime number, and also an integer."

在嵌套循环或分支语句中,为了明确指定break/continue中止哪一个循环或分支,可以使用标签功能

标签名:while 条件语句 {}

 break/continue 标签名

使用标签可以使程序更加清晰和易读。

你可能感兴趣的:(while,switch,swift,for,if)