美文网首页
Swift5.1学习随笔之条件语句、控制流程

Swift5.1学习随笔之条件语句、控制流程

作者: SAW_ | 来源:发表于2020-04-10 15:43 被阅读0次

if-else

let age = 4
if age >= 22 {
    print("单身多年了")
}
else if age >= 18 {
    print("长大的世界")
}
else if age >= 8 {
    print("滚去上学")
}
else {
    print("快乐童年")
}

1、if后面的条件可以省略小括号
2、条件后面的大括号不可以省略
3、if后面的条件只能是Bool类型

while

var num = 5
while num > 0 {
    print("num is \(num)")
    num -= 1
}
//打印了5次

1、while后面的条件可以省略小括号
2、条件后面的大括号不可以省略

repeat while

var num = -1
repeat {
    print("num is \(num)")
} while num > 0
//打印了1次

1、repeat while相当于C语言中的do-while

for
1、闭区间运算符: a...b 表示 a <= 取值 <= b

let names = ["a", "b", "b", "d"]
for i in 0...3 {
    print(names[i])
} //"a" "b" "b" "d"

let range = 1...3
for i in range {
    print(names[i])
} //"a" "b" "b"

let a = 1
let b = 2
for i in a...b {
    print(names[i])
} //"a" "b"

let a = 1
for i in a...3 {
    print(names[i])
}//"a" "b" "b"

//i 默认是let,根据需要可以声明为var
for var i in 1...3 {
    i += 5
    print(i)
} //6 7 8

//如果不需要使用到索引,可以设置成下划线 _
for _ in 1...3 {
    print("for")
} //打印了3次

2、半开区间运算符: a..<b 表示 a <= 取值 < b

for var i in 1..<5 {
    print(i)
} //1 2 3 4

3、for区间运算符用在数组上

let nums = ["a", "b", "b", "d"]
for num in nums[0...3] {
    print(num)
} //"a" "b" "b" "d"

4、单侧区间:让区间朝一个方向尽可能的远

let nums = ["a", "b", "b", "d"]
//从第3个索引开始,尽可能的访问数组的所有元素,最大到数组的最后一个索引,不会导致越界
for num in nums[2...] {
    print(num)
} //"b" "d"

//左边没有设置索引,默认从数组的第一个索引开始,不会越界
for num in nums[...2] {
    print(num)
} //"a" "b" "b"

for num in nums[..<2] {
    print(num)
} //"a" "b"
let range = ...5
range.contains(7) // false
range.contains(4) // true
range.contains(-3) // true

5、区间类型

//闭区间
let range1: ClosedRange<Int> = 1...3
//半开区间
let range2: Range<Int> = 1..<3
//单侧区间
let range3: PartialRangeThrough<Int> = ...5

6、字符、字符串也能使用区间运算符,但是默认不能用在for-in

let stringRange1 = "cc"..."ff" //ClosedRange<String>
stringRange1.contains("cb") //false
stringRange1.contains("dz") //true
stringRange1.contains("fg") //false

let stringRnage2 = "a"..."f"
stringRnage2.contains("d") //true
stringRnage2.contains("h") //false

//"\0"..."~"囊括了所有可能用到的ASCII字符
let characterRange: ClosedRange<Character> = "\0"..."~"
characterRange.contains("G") //true

7、带间隔的区间值

//tickMark的取值:从from:4开始,累加by:2,不超过through:11
for tickMark in stride(from: 4, through: 11, by: 2) {
    print(tickMark)
} // 4 6 8 10

8、标签语句

//对于多层for循环,单独的continue、break只能控制当层的for循环,加个outer标签语句,就能对应的控制需要的for循环层
outer: for i in 1...4 {
    for k in 1...4 {
        if k == 3 {
            continue outer
        }
        if i == 3 {
            break outer
        }
        print("i == \(i), k == \(k)")
    }
}
/*
 i == 1, k == 1
 i == 1, k == 2
 i == 2, k == 1
 i == 2, k == 2
 */

switch
1、case break后面不能写大括号{}

var num = 1
switch num {
case 1:
    print("1")
    break
case 2:
    print("2")
    break
default:
    print("其他")
    break
} //输出结果:1

2、默认可以不写brea,并不会贯穿到后面的条件

var num = 1
switch num {
case 1:
    print("1")
case 2:
    print("2")
default:
    print("其他")
} //输出结果:1

3、switch必须要保证能处理所有情况

var num = 1
switch num { // 此处报错:Switch must be exhaustive 解决方法:末尾加上default
case 1:
    print("num = 1")
case 2:
    print("num = 2")
}

4、casedefault后面至少要有一条语句,如果不想做任何事,加上break即可

var num = 1
switch num {
case 1:
    print("1")
case 2:
    print("2")
default:
    break
}

5、如果保证已经处理了所有情况,可以不需要使用default

enum Answer {
    case right, wrong
}
let answer = Answer.right
switch answer {
case .right:
    print("right")
case .wrong:
    print("wrong")
}

6、switch也支持Character String类型

let string = "老司机"
switch string {
case "老司机":
    fallthrough
case "带带我":
    print("老司机带带我")
default:
    break
} // "老司机带带我"

//可以多个条件写在一个case里面,用逗号隔开
switch string {
case "老司机", "带带我": 
    print("老司机带带我")
default:
    break
} // "老司机带带我"

7、区间匹配,元组匹配

let count = 69
switch count {
case 0:
    print("none")
case 1..<12:
    print("several")
case 12..<100:
    print("dozens of")
case 100..<1000:
    print("hundreds of")
default:
    print("many")
} // dozens of

let point = (0, 1)
switch point {
case (0, 0):
    print("origin")
case (_, 0): //可以用下划线_ 忽略某个值
    print("on the x-axis")
case (0, _):
    print("on the y-axis")
case (-2...2, -2...2):
    print("inside the box")
default:
    print("outside of the box")
} // "inside the box"

fallthrough

// fallthrough 用来实现贯穿效果
var num = 1
switch num {
case 1:
    print("num = 1")
    fallthrough
case 2:
    print("num = 2")
default:
    print("其他")
} //输出结果:
  //num = 1
  //num = 2

where

let point = (1, -1)
switch point {
case let (x, y) where x == y:
    print("on the line x == y")
case let (x, y) where x == -y:
    print("on the line x == -y")
case let (x, y):
    print("(\(x), \(y)) is just some arbitrary paoint")
} // one the line x == -y
var numbers = [10, 20, -10, -20, 30, -30]
var sum = 0
for num in numbers where num > 0 { //使用where来过滤num,使得正数才能相加
    sum += num
}
print(sum) // 60

相关文章

网友评论

      本文标题:Swift5.1学习随笔之条件语句、控制流程

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