if-else
let age = 4
if age >= 22 {
}
else if age >=18 {
}
else {
}
- if后面的条件都可以省略小括号
- 条件后面的大括号不可以省略
- if后面的条件只能是Bool类型
while
var num = 5
while num > 0 {
print("num is \(num)")
num -= 1
}
var num = -1
repat {
print("num is \(num)")
} while num >0//打印了一次
- repeat-while 相当于c语言中的do-while
- 这里不用num-- ,是因为从Swift3开始去掉了自增(++),自减(--)运算符
for
- 闭区间运算符:a...b, a<=取值<=b
let names = ["kkk","bbb","ccc","ddd"]
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])
}
image.png注释
网友评论