//TODO:条件语句
func if_Swich_ForIn_do(){
var e = ["d","aa","cc","bb"]
//for...in..
for s in e {
//if...else..
if s == "d" {
print("s == d")
}
else
{
print(s)
}
}
//while
let str:String = ""
var i = 0
while (i <= e.count-1) {
print(e[i])
i += 1
}
//递增
for index in 0 ..< 3 {
print("index is \(index)") //0 1 2
}
for y in 0 ... 3 {
print("y is \(y) \n") //0 1 2 3
}
for (index, i) in (1...10).enumerated(){
print(index)// 遍历索引 0 1 2 3 4 5 6 7 8 9
print(i) // 遍历元素 1 2 3 4 5 6 7 8 9 10
}
for i in (1...10).filter({ i in i % 2 == 0}) {
print(i)
//输出结果为1-10之间的偶数
}
///补充一个漂亮的语法糖///
for i in (1...10).filter({ $0 % 2 == 0}) {//swift闭包可以省略参数且用$0匿名,第二个参数则用$1,以此类推
print(i)
//输出结果为1-10之间的偶数
}
for t in (1...5).filter({$0 == 2}) {
print(t)
}
for t in 1...5 {
if t == 2{
print(t)
}
}
//递归
for index in (0...3).reversed() {
print(index) // 3 2 1 0
}
for index in (0..<3).reversed() {
print(index) // 2 1 0
}
//switch
let leng = (1,3)
switch leng {
case (0,1):
break
case (1,3):
break
case (_,3): //下 _ 线 匹配所有的值
break
default:
break
}
let cat = 30
switch cat {
case 10,20,1,2,3,4,5:
break
case 11,21,31,30:
break
default:
break
}
//continue 结束当次循环,立马进入下一次循环
for a in 1..<10 {
if a == 1
{
continue
}
}