1、if
常用的if也就三种
if bool{ } if bool{ } else{ } if bool{ } else if{ } else{ }
for index in 1...10{ println(index) }如果无需index的具体值,可以用下划线代替
for _ in 1...10{ println("hello hwc") }for in 最常用的还是用来遍历数组,字典
var dictionary = [1:"one",2:"two",3:"three"] for (key,value) in dictionary{ println("\(key)->\(value)") }当然,也可以用for的条件递增
for var index = 0;index < 10;++index{ println(index) }3、while
while condition{ statements } var i = 0 while i < 10{ println(i) i++ } do{ }while condition var i = 0 do{ println(i) i++ }while i < 10
4 switch
switch temp{ case value1: println(value1) case value2: println(value2) defalut: println(default) }swift中的switch语句可以不用break,会自动跳出
switch oenValue{ case value1: println("1") case value2...value4: println("234") default: println("default") }
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") }
let somePoint = (1, 1) switch somePoint { case let(x,y) where x == y: println("x is equal to y") default: println("x is not equal to y" ) }
for index in 1...10{ if index % 2 == 1{ continue } println(index) }break,立即结束当前控制流的执行
let x = 10 switch x { case 10: println("right") default: break }fallthrough贯穿,从swift中的一个case贯穿到下一个case
let x = 0 switch x { case 0: println(0) fallthrough case 1: println(1) case 2: println(2) default: println("defalult") }执行会发现输出0 1
var x = 1 var y = 1 firstLoop:while x < 5{ secondLoop:while y < 6{ //continue firstLoop or continue secondLoop } }
原文地址:http://blog.csdn.net/hello_hwc/article/details/40475667