Swift除了具备C所有的控制流结构外,还具备了oc中没哟的 for...in...结构方便遍历数组,字典等。
【循环】
swift的循环提供了四种结构:
<1>for...in:常用来遍历数组,类似与c#中的forearch
var myArray = ["string1",123,456] for i in myArray{ println("item is \(i)") }
var myArray = ["v1":"string1","v2":123,"v2":11.34] for (name,value) in myArray{ println("key name=\(name) key value=\(value)") }
<2>for...condition...increment:这个结构和oc的for一样。
for var i=0; i<10; i++{ println("index is \(i)") }
<3>while:和oc的while一样
while 1<2 { println("this is while loop") }
<4>do...while:和oc的do...while一样
do{ println("this is do while") }while 1<2
【条件语句】
<1>if...else
if 1<2 { ....... } else if 1<3{ ....... } else{ ....... }
<2>switch
var num:Int
switch num { case 1: case 2: println("num is 2") case 3: println("num is 3") case 4,5,6: println("num is 4 or 5 or 6") default: println("num is not 1 or 2 or 3") }
时间: 2024-11-15 23:01:43