if-else
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")
}
if后面的条件只能是Bool类型
while
var num = 5
while num > 0
{
print("num is \(num)")
num -= 1
}
var num = -1
repeat
{
print("num is \(num)")
} while num > 0
1.repeat-while相当于C语言中的do-while
2.这里不用num-- ,是因为从Swift3开始,去除了自增( ++ )、自减( -- )运算符
for
闭区间运算符:a…b, a <= 取值 <= b
let names = ["Anna","Alex","Brian","Jack"]
for i in 0...3
{
print(names [i])
}
let range = 1...3
for i in range
{
print(names [i])
}
let a = 1
var b = 2
for i in a...b
{
print(names [i])
}
for i in a...3
{
print(names [i])
}
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 names = ["Anna", "Alex", "Brian", "Jack"]
for name in names [0...3]
{
print(name)
}
单侧区间 :让区间朝一个方向尽可能的远
for name in names [2...]
{
print(name)
}
for name in names [...2]
{
print(name)
}
for name in names [..<2]
{
print(name)
}
let range = ...5
range.contains(7)
range.contains(4)
range.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"
stringRange1.contains("cb")
stringRange1.contains("dz")
stringRange1.contains("fg")
let stringRange2 = "a"..."f"
stringRange2.contains("d")
stringRange2.contains("h")
let characterRange: ClosedRange<Character> = "\0"..."~"
characterRange.contains("G")
带间隔的区间值
let hours = 11
let hourInterval = 2
for tickMark in stride(from: 4, through: hours, by: hourInterval)
{
print(tickMark)
}
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
}
var number = 1
switch number
{
case 1:
print("number is 1")
case 2:
print("number is 2")
default:
print("number is other")
}
fallthrough
使用fallthrough可以实现贯穿效果
var number = 1
switch number
{
case 1:
print("number is 1")
fallthrough
case 2:
print("number is 2")
default:
print("number is other")
}
switch必须要保证能处理所有情况
enum Answer { case right, wrong }
let answer = Answer.right
switch answer
{
case Answer.right:
print("right")
case Answer.wrong:
print("wrong")
}
复合条件
switch也支持Character、 String类型
let string = "Jack"
switch string
{
case "Jack":
fallthrough
case "Rose":
print("Right person")
default:
break
}
let character: Character = "a"
switch character
{
case "a", "A":
print("The letter A")
default:
print("Not the letter A")
}
switch string
{
case "Jack", "Rose":
print("Right person")
default:
break
}
区间匹配、元组匹配
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")
}
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")
}
值绑定
let point = (2, 0)
switch point
{
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 a y value of \(y)")
case let (x, y):
print("somewhere else at (\(x), \(y))")
}
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 point")
}
var numbers = [10, 20, -10, -20, 30, -30]
var sum = 0
for num in numbers where num > 0
{
}
print(sum)
标签语句
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)")
}
}