//if 后面只能是Bool类型
let age = 4
if age >= 22 {
print("Get married")
} else if age >= 18 {
print("Being a adult")
} else if age >= 7 {
print("go to school")
} else {
print("just a child")
}
var num = 5
while num > 0 {
print("num is \(num)")
num -= 1
}
//从 Swift3开始,去除了自增(++),自减(--)运算符
var num2 = -1
repeat {
print("num2 is \(num2)")
} while num > 0
let names = ["Anna", "Alex", "Brian", "jack"]
for i in 0...3 {
print(names[i])
}
for name in names[1...3] {
print(name)
}
let range = 1...3
for i in range {
print(names[i])
}
//i 默认是let,有需要时可以申明为var
for var i in 1...3 {
i += 5
print(i)
}
for _ in 1...3 {
print("for")
}
//半开区间:a..<b, 意思是a<=取值<b
for i in 1..<5 {
print(i)
}
//:#### for - 区间运算符用在数组上
let names1 = ["Anna", "Alex", "Brian", "Jack"]
for name in names1[3...3] {
print(name)
} //Jack
//单侧区间:让区间朝一个方向尽可能的远, 在数组里面,是有限定的,从0开始,最大到底数组的长度,最后一个字符
for name in names[2...] {
print(name)
} //Brian, Jack
for name in names[...2] {
print(name)
} //Anna, Alex, Brian
for name in names[...2] {
print(name)
} //Anna, Alex
//与上面rang放到数组里面形成对比,在外面范围会更大
let rang = ...5
print(rang.contains(7))
print(rang.contains(3))
print(rang.contains(-3))
//:#### 区间类型
let range1: ClosedRange<Int> = 1...3 //闭区间
let range2: Range<Int> = 1..<3 //半开区间
let range3: PartialRangeThrough<Int> = ...5
//字符,字符串也能使用区间运算符,但默认不能用在for-in中
let stringRange1 = "cc"..."ff"
print(stringRange1.contains("cb")) //false
print(stringRange1.contains("dz")) //true
print(stringRange1.contains("fg")) //false
let stringRange2 = "a"..."f"
print(stringRange2.contains("d")) //true
print(stringRange2.contains("h")) //false
let str = "a" //"a"默认为string类型
let char: Character = "a" //如果想申明为字符类型,必须要指定类型才行
// \0到~囊括里所有可能要用到的ASCII字符
let characterRange: ClosedRange<Character> = "\0"..."~"
print(characterRange.contains("G")) //true
//:#### 带间隔的区间
let hours = 11
let hourInterval = 2
// tickMark 的取值:从4开始,累加2,不超过11
for tickMark in stride(from: 4, through: hours, by: hourInterval) {
print(tickMark)
} // 4 6 8 10
//switch
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
} //number is 1
//case, default后面不能写大括号{}
var number2 = 1
switch number2 {
case 1:
print("number is 1");
case 2:
print("number is 2");
default:
print("number is other");
}
//默认可以不写break,并不会贯穿到后面的条件
//fallthrough
//使用fallthrough可以实现贯穿效果
//注意:switch必须要保证能处理所有情况
var number3 = 1
switch number3 {
case 1:
print("number is 11")
fallthrough
case 2:
print("number is 22")
default:
print("number is other")
}
//case,default后面至少有一条语句
//如果不想做任何事情,加个break即可
var number4 = 1
switch number4 {
case 1:
print("number is 11")
fallthrough
case 2:
print("number is 22")
default:
break
}
//如果可以保证已处理所有情况,也可以不必使用default
enum Answer {case right, wrong}
let answer = Answer.right
switch answer {
case Answer.right:
print("right")
case Answer.wrong:
print("wrong")
} //right
//或者如果可以确定answer是Ansewer类型,可以省略Answer
switch answer {
case .right:
print("right")
case .wrong:
print("wrong")
} //right
//复合条件
//switch也可以支持Character, String类型
let string = "Jack"
switch string {
case "Jack":
fallthrough
case "Rose":
print("Right person")
default:
break
} //Right person
//上面的逻辑也可以写成下面这样
switch string {
case "Jack","Rose":
print("Right person")
default:
break
} //Right person
//如果是字符需要指定类型 Character
let character: Character = "a"
switch character {
case "a","A":
print("The letter A")
default:
print("Not the letter A")
}
//区间匹配
let count = 62
switch count {
case 0:
print("none")
case 1..<5:
print("a few")
case 5..<12:
print("several")
case 12..<100:
print("dozens of")
case 100..<1000:
print("hundreds of")
default:
print("many")
} //dozens of
//元组匹配
//可以使用下划线 _ 忽略某个值
//关于case匹配问题,属于模式匹配(Pattern Matching)范畴,后面会再次详细展开讲解
let point = (1, 1)
switch point {
case (0, 0):
print("the 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
//值绑定
//let也可以换成var
let point2 = (2, 0)
switch point2 {
case (let x, 0):
print("on the x-axis with an x value of \(x)")
case (0, let y):
print("on the y-axis with an y value of \(y)")
case let (x, y):
print("somewhere else at (\(x), \(y))")
} //on the x-axis with an x value of 2
//where
let point3 = (1, -1)
switch point3 {
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 point")
} // on the line x == -y
//将所有正数加起来
var numbers = [10, 20, -10, -20, 30, -30]
var sum = 0
for num in numbers where num > 0 {
sum += num;
}
print("sum is \(sum)")//sum is 60
//标签语句
//可以标记某一个for循环,然后针对性的指定终止某个循环
outer: for i in 1...4 {
outer1:for k in 1...4 {
if k == 3 {
continue outer1
}
if i == 3 {
continue outer
}
print("i == \(i), k == \(k)")
}
}
Swift: 03-流程控制
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- 一、流程控制 1.1、if 语句let age = 5if age > 10 { print("\(age...
- Swift5 基础教程与进阶合集[https://www.jianshu.com/p/7d0506aeef29] ...