if语句
判断条件不需要小括号,条件执行体必须要放在花括号中
var int6 : Int = 6
if int6 > 0
{
print(int6)
}
else
{
print("666666666")
}
switch语句
switch中不需要强制写break语句,但是在每个case里必须有一条语句
fallthrough可以在这个case块执行完成之后直接执行下
var score = "c"
switch score
{
case "A","a":
print("优秀")
case "B":
print("良好")
case "c":
fallthrough
case "C":
print("中")
case "D":
print("及格")
default:
print("成绩输入错误")
}
break用来在不需要写执行语句的时候结束switch语句
var score1 = 1
switch score1
{
case 0:
print("0")
case 1:
print("1")
default:
break
}
case后的判断条件可以是多个值,也可以是一个判断范围,也可以是一个元组
var score2 = 30
switch score2
{
case 0...9:
print("9")
case 10...30:
print("30")
default:
break
}
var somepoint = (5,5)
switch somepoint
{
case (0,0):
print("")
case (_,0):
print("")
case (0,_):
print("")
case (0...10,0...10):
print("")
default:
break
}
case后面可以用来绑定符合条件的的值
case后的值后面可以再加上where判断
var score3 = 110
switch score3
{
case 0...60:
print("try hard")
case 61...80:
print("good")
case 81...100 where score3<95:
print("perfet")
case 81...100 where score3>=95:
print("unbelievble")
case let sco:
print("get out with your (sco)")
}
while语句
var count1 = 0
while count1 < 0
{
print(count1)
count1++
}
repeat语句
var count2 = 0
repeat
{
print(count2)
count2++
}while count2 < 0
for循环语句
for var count3 = 0 ; count3 < 5 ; count3++
{
print("ccc")
}
for var count4 = 0, count5 = 1 ; count4<100 ; count4++
{
print(count4 + count5)
}
for in 语句
let books = ["swift","oc","c","c++"]
for index in 0..<books.count
{
print(index)
}
//0123
for in 循环的第一个参数无需声明,系统会在每次循环开始时隐式声明一个常量,所以,也不能再执行代码中对第一个参数赋值
如果循环体内无须正在遍历的元素的值,程序可使用下划线代替常量名来忽略访问元素的值
for _ in 1...5
{
print("sssss")
}
continue,break,return
break可以结束一个分支语句或循环语句
一般break用来结束当前循环,如果有嵌套循环,可以给循环一个标示,break可以通过这个标示直接结束外层循环
out: for var count = 5 ; count < 7 ; count++
{
for var count1 = 1 ; count1 < 3 ; count1++
{
print("count1")
if count1 == 2
{
break out;
}
}
}
continue用来结束本次循环,直接进行下一次循环,也可以通过标签来结束外层循环
return用来结束一个函数或方法