for循环
Swift提供了两种for循环语句,一种是C语言的for循环:条件递增,这种方式在SWift3.0中遗弃了,现在Swift主要推荐for-in循环
//在for-in语句中,..<符号表示数组范围0~10之间,但是并不包含10,如果循环中包含10,可以使用...10
for index in 0..<10 {
print("index:\(index)")
}
//循环遍历数组
let names = ["小明","小红"]
for index in names {
print("name:\(index)")
}
//遍历元组
let yuanz = [(12,"age"),(13,"age")]
for index in yuanz {
print("name:\(index.0)")
}
//遍历字典
var dic=["name":"小明","age":"12"]
for index in dic {
print("index:\(index)")
}
while循环语句
Swift的while循环语句,和OC的while语句非常的相似,用于重复执行某个代码块
var index = 0
while index<5
{
index+=1
print("index:\(index)")
}
repeat-while循环语句(do-while语法一致)
var a=0
repeat
{
a+=1
print("a:\(a)")
}
while a<5
if条件判断语句
if(3<4){
print("呵呵")
}
switch条件判断语句
let time = 10
switch time {
case 9:
print("这是9")
case 10:
print("这是10")
case 11,12,14:
print("这是毛线")
default:
print("这是毛线")
}
continue语句
for index in 0...10 {//打印的结果是没有3的
if index==3 {
continue
}
print("index:\(index)")
}
fallthrough语句
通过该语句,可以允许Swift代码在执行完一个case分支后,调到下一个case中
let age = 10
switch age {
case 9:
print("这是9")
fallthrough
case 10:
print("这是10")
fallthrough
case 11,12,14:
print("这是毛线")
default:
print("这是毛线")
}
网友评论