Swift控制流

let base = 3
let pow = 10
var answer = 1
for _ in 1…pow{

answer *= base

}

for index in 0 ..< 3 {

}

let test = “c”

switch test{
case “a”: print(“(test) is equ to a” )
case “b”,”c”,”d”:print(“是bcd 中的其中一個”) //支持多個匹配

case “b”: print(“(test) is equ to b”)
default :print(“not found”)
}

//repeat {// 類 oc 中 do while
// print(“a”)
//
//
//}while 2 > 0// 爲真執行輸出 , 爲假退出循環.

let somePoint = (1, 1)
switch somePoint {
case (0, 0):
print(“(0, 0) is at the origin”)
case (_, 0):// x值不論
print(“((somePoint.0), 0) is on the x-axis”)
case (0, _):// y值不論
print(“(0, (somePoint.1)) is on the y-axis”)
case (-2…2, -2…2):
print(“((somePoint.0), (somePoint.1)) is inside the box”)
default:
print(“((somePoint.0), (somePoint.1)) is outside of the box”)
}
// prints “(1, 1) is inside the box”

let anotherPoint = (2, 0) //元組
switch anotherPoint {
case (let x, 0):
print(“on the x-axis with an x value of (x)”) //在 x軸上
case (0, let y):
print(“on the y-axis with a y value of (y)”) //在 y軸上
case let (x, y):
print(“somewhere else at ((x), (y))”) //在 象限內.
}
// prints “on the x-axis with an x value of 2

//continue 如果爲真 直接跳過這次循環。 進入下次循環。。.

// break 跳出整個循環 循環終止.

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章