//: Playground - noun: a place where people can play import UIKit // fallthrough // fallthrough会在当前case执行完之后继续下一个case // 如果在下一个case中声明了变量, 则不能使用fallthrough var coordinate = (1, 0) switch coordinate { case (0, 0): print("It‘s at origin!") fallthrough case (_, 0): print("(\(coordinate.0), 0) is on the x-axis.") fallthrough case (0, _): print("(0, \(coordinate.1)) is on the y-axis.") fallthrough case (-2...2, 0...2): print("the coordinate is (\(coordinate.0), \(coordinate.1))") default: print("(\(coordinate.0), \(coordinate.1)) is just an ordinary coordinate") } // break for var i = 0; i < 10; i++ { print(i) for var j = 0; j < 10; j++ { print(j) if i + j == 10 { print(i + j) break // 跳出break所在的循环体 } } } // continue var str = "askdjfskldjfdifjhget,n,mcnmvxcvawperiuzxncv,." var num = 0; for string in str.characters { switch string { case ",", ".": num++ default: continue; } } // 初始化二维数组 var board = Array<Array<Int>>() for i in 0..<10 { // count: 数组初始化后的元素个数 repeatedValue: 数组的元素的值 board.append(Array(count: 10, repeatedValue: 0)) } let randx = Int(arc4random()%10) let randy = Int(arc4random()%10) board[randx][randy] = 1 board // mainloop用于标记某个循环, 有了标记, break就可以直接跳出某个指定的循环了. eg: var i = 0, j = 0 mainFor:for i = 0; i < 10; i++ { // mainFor这个标记是可以自定义的 for j = 0; j < 10; j++ { if board[i][j] == 1 { break mainFor } } } print("board[\(i)][\(j)] = 1")
时间: 2024-10-20 09:54:21