3流程控制
博客专区 > 阿元 的博客 > 博客详情
3流程控制
阿元 发表于11个月前
3流程控制
  • 发表于 11个月前
  • 阅读 1
  • 收藏 0
  • 点赞 0
  • 评论 0

标题:腾讯云 新注册用户域名抢购1元起>>>   

##3.1 for 循环 ###for - in 循环结构

for loopVariable in startNumber ...endNumber

“...”告诉Swift起始数字和结束数字指定的是范围

 1> var loopCounter : Int = 0
loopCounter: Int = 0
  2> for loopCounter in 1...5{
  3.     print("\(loopCounter)times \(loopCounter * loopCounter)")
  4. }
1times 1
2times 4
3times 9
4times 16
5times 25

###另一种指定范围语法"..<"

  8> for loopCounter in 1..<5 {
  9.     print("\(loopCounter)times \(loopCounter * loopCounter)")
 10. }
1times 1
2times 4
3times 9
4times 16

"..<"比结束数字小1,用作数组索引时比较直观(因为数组索引从0开始)

###老式 for 循环 Swift3.0中去掉 需要stride方法的协助

 13> for i in stride (from:10 ,through: 0 , by: -1) {
 14.     print("\(i)")
 15. }
10
9
8
7
6
5
4
3
2
1
0

reversed反转

 16> for i in (0...10).reversed() {
 17.     print("\(i)")
 18. }
10
9
8
7
6
5
4
3
2
1
0

###简写

 19> var anotherLoopCounter = 3
anotherLoopCounter: Int = 3
 20> anotherLoopCounter += 2
 21> anotherLoopCounter
$R0: Int = 5
 22> anotherLoopCounter -= 3
 23> anotherLoopCounter
$R1: Int = 2
 24> anotherLoopCounter++
error: repl.swift:24:19: error: '++' is unavailable: it has been removed in Swift 3
anotherLoopCounter++
                  ^~
                   += 1

递增++,递减--,swift3.0移除 移除原因参考http://swift.gg/2016/03/30/swift-qa-2016-03-30/

##3.2 if 语句

var trafficLight = "Greed"
if trafficLight != "Greed" {
	print("Stop!")
}else{
	print("Go!")
}

比较运算符 “==” 等于 “!=” 非等于 “>” 大于 “<” 小于 “>=” 大于等于 "<=" 小于等于

比较字符串

let tree1 = "Oak"
let tree2 = "Pecan"
let tree3 = "Maple"
let treeCompare1 = tree1 > tree2
let treeCompare2 = tree2 > tree3

###if ,else if

var treeArray = [tree1, tree2 , tree3]
for tree in treeArray{
	if tree == "Oak"{
		print("Furniture")
	}else if tree == "Pecan"{
		print("Pie")
	}else if tree == "Maple"{
		print("Syrup")
	}
}

##3.3switch 字符串作为判断

treeArray += ["Cherry"]
treeArray += ["apple"]
for tree in treeArray{
	switch tree {
	case "Oak":
		print("Furniture")
	case "Pecan","Cherry":
		print("Pie")
	case "Maple":
		print("Syrup")
	default:
		print("Wood")
	}
}
Furniture
Pie
Syrup
Pie
Wood

一般数字做判断

var position = 7
switch position {
case 1:
	print("\(position)st")
case 2:
	print("\(position)nd")
case 3:
	print("\(position)rd")
case 4...9:
	print("\(position)th")
default:
	print("Not coverd")
}
7th

swift中移除OC中switch的break,判断匹配到的语句后直接跳出

##3.4while循环

var base = 2
var target = 100
var value = 0
while value < target {
	value += base;
}

do-while 更换为repeat-while(第一次执行完,在判断是否继续执行while循环)

repeat{
	value += base
}while value < target

break提前结束循环

var speedLimit = 75
var carSpeed = 0

while (carSpeed < 100){
	carSpeed += 1
	switch carSpeed {
	case 0...30:
		print("low Speed\(carSpeed)")
	case 31...50:
		print("Normal Speed\(carSpeed)")
	case 51...75:
		print("Litter Faset\(carSpeed)")
	default:
		print("Too Fast must Stop")
	}
	//break 提前结束循环
	if(carSpeed > speedLimit){
		break
	}
}
共有 人打赏支持
粉丝 5
博文 151
码字总数 95488
×
阿元
如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
* 金额(元)
¥1 ¥5 ¥10 ¥20 其他金额
打赏人
留言
* 支付类型
微信扫码支付
打赏金额:
已支付成功
打赏金额: