//****************条件语句*****************
var legcount=19
//变量,可以是整形,也可以是字符串
switch legcount {
case 0:
println("AAA")
break //swift中可以不写
case 1:
println("BBB")
case 2,3,4,5,6:
println("DDD"); //";"可以不写
case 10..<20: //可以用区间
println("EEE");
fallthrough //贯穿这个分支,继续下面的分支
default: //如果上述的case已经把情况覆盖全了,可以不写default
println("CCC")
}
//动作:
//varsender=Sender
//switchsender{
// casebutton:
// println()
// caseusernameTextField:
// println()
// default:
// println()
//}
//使用下划线(_)来匹配可能的值
let somePoint = (1, 1)
let闭包 = {
println("somePoint = \(somePoint)")
}
func 函数(){
println("somePoint = \(somePoint)")
}
switch somePoint {
case (0 ,0) :
闭包()
case (_, 0) :
闭包()
case (0, _) :
闭包()
case (_, _) :
闭包()
函数()
}
//值绑定
let anotherPoint = (2,3)
switch anotherPoint {
case (let x, 0) :
println(" x = \(x)")
case (0, let y) :
println(" y = \(y)")
case (let x, let y) :
println(" x = \(x), y = \(y)")
}
//where语句
let theotherPoint = (3,3)
func printXY(x:Int, y:Int){
print(" x = \(x), y = \(y) ")
}
switch theotherPoint {
case let (x, y) where x<y :
printXY(x, y);
println("x<y")
case let (x, y) where x>y :
printXY(x, y);
println("x>y")
case let (x, y) where x==y :
printXY(x, y);
println("x=y")
default : println("default")
}
//****************循环*****************
//标签
var i=0, j=0
FirstLoop: while i < 3 {
println("FirstLoop i = \(i++), ****** j = \(j++)")
var k=0, g=0
SecondLoop : while k < 3 {
println("SLoop k = \(k++)")
if g>0 {
//break FirstLoop
//break SecondLoop
//continue FirstLoop
//continue SecondLoop
//continue
//break
}
println("SLoop g = \(g++)")
}
}
var loops=0
while (loops<5){
println("while=\(loops++)")
}
for loops=1;loops<5;++loops{
println("for=\(loops)")
}
for number005 in 1..<5{ //1~4
println("number005=\(number005)")
}
//不需要知道范围内的项目,可以用_代替number005
for _ in 1..<5 {
println("不需要知道范围内的项目,可以用_代替number005")
}
forstr005 in "常量005"{
println("str005=\(str005)")
}
//同一个数组或字典都可以包含不同类型的值,如下
vardict005=["哈哈":"haha","呵呵":11]
for (A005,B005) in dict005{
println("A=\(A005),B=\(B005)")
}