美文网首页
认识Swift系列2之流程控制

认识Swift系列2之流程控制

作者: Niuszeng | 来源:发表于2019-07-11 19:06 被阅读0次

Swift中的if、while、for、区间运算符、标签语句

  • if-else 语句(if 条件只能是Bool类型)
let a = 10
// if 后边条件只能是bool类型
// 一下语句报错
//    if a {
//        print(a)
//    }
// 这样才行,后边小括号可以省略,但是大括号不可省略
//    if a > 5  print(a)
if (a > 5) { print(a) }
if a > 5 { print(a) }
  • while循环(while 条件只能是Bool类型)
// while之后也职能是bool类型
var b = 5
while b > 2 {
    print("b is \(b)")
    b -= 1
}

// repeat-while,类似于do-while,第一次先执行再检查条件
var c = 1
repeat{
    print("c is \(c)")
    c -= 1
} while c > 2
// note:swift3去除了自增(++)和自减(--)运算符
  • for循环,这里用一个例子
let arr = ["aaa", "bbb", "ccc"]
for item in arr {
    print(item)
}
  • 区间运算符
// 区间运算符1
let range1 = 0...5 // [0, 5]
// let range2 = 0<..5 // (0, 5] // 貌似没有这种
let range3 = 0..<5 // [0, 5)
let range4 = 0...5 // [0, 5]
let range5 = 0..<5 // [0, 5)
let range6 = 0... // [0, ∞)
let range7 = ...5 // (∞, 5]

for item in range1 {
    print(item)
}


// 区间运算符2
let range8: ClosedRange<Int> = 1...3 // [1, 3]
let range9: Range<Int> = 1..<3 // [1, 3)
let range10: PartialRangeThrough<Double> = ...5.0 // (∞, 5.0)
let range11: PartialRangeFrom<Int> = 3... // [3, ∞)
let range12: PartialRangeUpTo<Double> = ..<3.0 // (∞, 3.0)

// 区间运算符3(默认不能在for-in使用)
let stringRange1 = "aa"..."zz" // ["aa", "zz"]
let stringRange2 = "aa"..<"zz" // ["aa", "zz")
let stringRange3 = "aa"... // ["aa", ∞)
stringRange1.contains("zz") // true
/** 报错
 for item in stringRange1 {
 print(item)
 }
 */

// 区间运算符4(带间隔)
let rangeInterval1 = stride(from: 20, to: 26, by: 1)
let rangeInterval2 = stride(from: 20, to: 26, by: 2)
let rangeInterval3 = stride(from: 26, to: 20, by: -2)

print("-------")
for item in rangeInterval1 {
    print(item)
}

print("-------")
for item in rangeInterval2 {
    print(item)
}

print("-------")
for item in rangeInterval3 {
    print(item)
}
  • switch语句

可以不写break(不会穿透),打宝石每个case:/default:后必须有一条语句
如果case:/default:不想做任何事,使用加个break即可
需要穿透使用 fallthrough

var number = 1
switch number {
case 1:
    print("number is 1")
    break
case 2:
    print("number is 2")
    break
default:
    print("number is other")
    break
}

// 写法等价:
switch number {
case 1:
    print("number is 1")
case 2:
    print("number is 2")
default:
    print("number is other")
}

// 穿透
switch number {
case 1:
    print("number is 1")
    fallthrough
case 2:
    print("number is 1 or 2")
default:
    print("number is other")
}

// 如果已经保证所有情况都处理了,可以不写default:,否则报错
enum Enum {case a, b, c }
let e = Enum.a
switch e {
case .a:
    print("e is a")
case .b:
    print("e is a")
case .c:
    print("e is a")
}
// 否则写成如下
switch e {
case .a:
    print("e is a")
default:
    print("e is b or c")
}

// switch也支持复合条件,支持Character、String等
let string = "jack"
switch string {
case "rose", "test":
    print("rose is rose/test")
default:
    print("rose not is rose")
}

let character: Character = "b"
switch character {
case "a", "b", "c":
    print("character is a/b/c")
default:
    print("character is unknown")
}

// switch支持区间、元组匹配、值绑定
let count = 62
switch count {
case 1..<5:
    print("count in 1..<5")
case 5...70:
    print("count in 5...70")
default:
    print("count in 70...")
}

let point = (1, 2)
switch point {
case (0, 0):
    print("point is (0, 0)")
case (1, 2):
    print("point is (1, 2)")
default:
    print("point is none")
}

switch point { // 值绑定
case (let x, 0):
    print("point is (\(x), 0)")
case (0, let y):
    print("point is (x, \(y))")
case (let x, let y):
    print("point is -- (\(x), \(y)))")
}

// where 条件
let point1 = (1, -1)
switch point1 {
case let (x, y) where x == y:
    print("point is where (\(x), \(y)))")
case let (x, y) where x == -y:
    print("point is where (\(x), \(y)))")
default:
    print("point is where none")
}
  • 标签语句
func test_label() {
    tempTag: for i in 1...4 {
        for k in 1...5 {
            if k == 3 {
                // 内部循环每次执行到 k==3 就切换到外层循环
                continue tempTag
            }
            
            if i == 3 {
                // 外层循环到 i==3 就终止(双层循环终止)
                break tempTag
            }
            print("i == \(i), k == \(k)")
        }
    }
}
test_label()

相关文章

网友评论

      本文标题:认识Swift系列2之流程控制

      本文链接:https://www.haomeiwen.com/subject/kocjkctx.html