Swift学习:11.控制流

前端之家收集整理的这篇文章主要介绍了Swift学习:11.控制流前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

1.循环

var singers = ["","",27)">丁",27)">克"]

for singerinsingers{}

for iin0..<singers.count{}

while(条件语句){}

do{}while(条件)

2.Switch case 专题

switchstudents{

case ("北大",2007):print("天之骄子")

case ("重邮",2006...2010):print("就业率最高")

case ("北大",2005..<2009):print("拔尖")

default:print("")

}

_):print("天之骄子")

_):print("就业率最高")

default:print("")

}


3.值绑定和条件绑定

var position = (55,45)

switch position{

case let(x,55):

println("[\(x),55]")

caselet(x,y)where x == y:

println("[\(x),\(y)]")

caselet(x,y)where x <45 && y >35:

println("[\(x),\(y)]")

default: println("")

}

4.打破流程控制

outFor:for ain1...5{

for iin1...10{

if i ==2{

break outFor

}

print("\(i)")

}

}

note:switch case 中可以使用fall through贯穿

5.给数组添加序号

for (index,user) in enumerate(userList){

}


5.示例代码

forvar i =0 ; i <100 ; i++ {

}

forvar index =0; index <10; ++index{

}

var i =1;

var j =2;


let base =3

let power =10

var answer =1

for_in1...power{

answer = answer * base

}

println("\(answer)")


let finalSquare =25

var board = [Int](count:finalSquare +1,repeatedValue:0)

//var board = Array(count: finalSquare,repeatedValue: 0)


board[03] = +08;board[06] = +11;board[09] = +09;board[10] = +02

board[14] = -10;board[19] = -11;board[22] = -02;board[24] = -08


var square =0

var diceRoll =0

/*

while square < finalSquare{

// 掷骰子

if ++diceRoll == 7{

diceRoll = 1

}

square += diceRoll

if square < board.count{

square += board[square]

}

*/

gameLoop:whilesquare !=finalSquare{

if ++diceRoll ==7{

diceRoll =1

}


switchsquare +diceRoll{

casefinalSquare:

break gameLoop

caselet numberwhere number >finalSquare:

continue gameLoop

default:

square += diceRoll

square += board[square]

}

}

println("game over")

let someCharactor:Character ="u"

switch someCharactor{

case "a","e","i","o","u":

println("someCharactor is yuanyin")

case "b","c":

println("someCharactor is fuyin")

default:

println("someCharactor is not a charactor")

}


let somePoint = (1,1)

switch somePoint{

case (0,0):

println("Origin")

case (0,_):

println("y")

case (_,0):

println("x")

case (-2...2,-2...2):

println("正方形内")

default:

正方形外")

}

switch somePoint{

caselet (x,y)where x == y:

println("x=y")

caselet (x,y)where x == -y:

println("x=-y")

caselet (x,y):

println("other")

}

原文链接:https://www.f2er.com/swift/327162.html

猜你在找的Swift相关文章