let age =4if age >=22{print("Get married")}elseif age >=18{print("Being a adult")}elseif age >=7{print("Go to school")}else{print("Just a child")}/*
1.if后面的条件可以省略小括号
2.条件后面的大括号不可以省略
*/if后面的条件只能是Bool类型
while
var num =5while num >0{print("num is \(num)")
num -=1}// 打印了5次
var num =-1repeat{print("num is \(num)")}while num >0// 打印了1次
let names =["Anna","Alex","Brian","Jack"]for i in0...3{print(names [i])}// Anna Alex Brian "Alex", "Brian", "Jack"]
let range =1...3for i in range
{print(names [i])}// Alex Brian Jack
let a =1var b =2for i in a...b
{print(names [i])}// Alex Brianfor i in a...3{print(names [i])}// Alex Brian Jack
// i默认是let,有需要时可以声明为varforvar i in1...3{
i +=5print(i)}// 6 7 8
for_in1...3{print("for")}// 打印了3次
半开区间运算符:a…<b, a <= 取值 < b
for i in1..<5{print(i)}// 1 2 3 4
for – 区间运算符用在数组上
let names =["Anna","Alex","Brian","Jack"]for name in names [0...3]{print(name)}// Anna Alex Brian Jack
单侧区间 :让区间朝一个方向尽可能的远
for name in names [2...]{print(name)}// Brian Jackfor name in names [...2]{print(name)}// Anna Alex Brianfor name in names [..<2]{print(name)}// Anna Alex
let range =...5
range.contains(7)// false
range.contains(4)// true
range.contains(-3)// true
区间类型
let range1:ClosedRange<Int>=1...3let range2:Range<Int>=1..<3let range3:PartialRangeThrough<Int>=...5
var number =1switch number {case1:print("number is 1")breakcase2:print("number is 2")breakdefault:print("number is other")break}// number is 1//case、 default后面不能写大括号{}
var number =1switch number
{case1:print("number is 1")case2:print("number is 2")default:print("number is other")}// number is 1//默认可以不写break ,并不会贯穿到后面的条件
fallthrough
使用fallthrough可以实现贯穿效果
var number =1switch number
{case1:print("number is 1")fallthroughcase2:print("number is 2")default:print("number is other")}// number is 1// number is 2
let string ="Jack"switch string
{case"Jack":fallthroughcase"Rose":print("Right person")default:break}// Right person
let character:Character="a"switch character
{case"a","A":print("The letter A")default:print("Not the letter A")}// The letter A
switch string
{case"Jack","Rose":print("Right person")default:break}// Right person
区间匹配、元组匹配
letcount=62switchcount{case0:print("none")case1..<5:print("a few")case5..<12:print("several")case12..<100:print("dozens of")case100..<1000:print("hundreds of")default:print("many")}// dozens of
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//可以使用下划线 _ 忽略某个值//关于case匹配问题,属于模式匹配( Pattern Matching )的范畴
值绑定
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)")caselet(x, y):print("somewhere else at (\(x), \(y))")}// on the x-axis with an x value of 2//必要时let也可以改为var
where
let point =(1,-1)switch point
{caselet(x, y)where x == y:print("on the line x == y")caselet(x, y)where x ==-y:print("on the line x == -y")caselet(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 =0for num in numbers where num >0{// 使用where来过滤num sum += num}print(sum)// 60
标签语句
outer:for i in1...4{for k in1...4{if k ==3{continue outer
}if i ==3{break outer
}print("i == \(i), k == \(k)")}}
if-elselet age = 4if 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")}/*1.if后面的条件可以省略小括号2.条件后面的大括号不可以省略*/if后面的条件只能是Bool