// Control Transfer: fallthrough break continue import UIKit //fallthrough let coordinate = (0,0) switch coordinate { case (0,0): println("It's at origin!") fallthrough case (_,0): println("(\(coordinate.0),0) is on the x-axis.") case (0,_): println("(0,\(coordinate.1)) is on the y-axis.") fallthrough case (-2..<2,-2..<2): println("(\(coordinate.0),\(coordinate.1)) is near the origin.") fallthrough default: println("(\(coordinate.0),\(coordinate.1)) is an ordinary coordinate.") } //break var primes = [2] for var i = 3 ; i <= 100 ; i += 2 { var isPrime = true for aPrime in primes { if i%aPrime == 0 { isPrime = false break } } if isPrime { primes += [i] } } for aPrime in primes { //println(aPrime) } //break let test_str = "This is a test sentence." var num = 0 let str = test_str.lowercaseString for c in str { switch c { case "a","e","i","o","u": break case "b","c","d","f","g","h","j","k","l","m","n","p","q","r","s","t","v","w","x","y","z": num++ default: break } } num //continue let str_long = "floccinaucinihilipilification" var sum = 0 for c in str_long { switch c { case "a","e","i","o","u": continue default: sum++ } } sum //mainloop var board = Array< Array<Int> >()//二维数组 for i in 0..<10 { board.append(Array(count:10,repeatedValue:0)) } let randx = Int(arc4random()%10) let randy = Int(arc4random()%10) board[randx][randy] = 1 board var i = 0 , j = 0 mainloop: for i = 0 ; i < 10 ; j++ { for j = 0 ; j < 10 ; j++ { if board[i][j] == 1 { break mainloop } } } println("board[\(i)][\(j)] = 1")
示例代码