@@ -796,13 +796,13 @@ one or more values of the same type.
796796
797797```
798798switch <#some value to consider#> {
799- case <#value 1#>:
800- <#respond to value 1#>
801- case <#value 2#>,
802- <#value 3#>:
803- <#respond to value 2 or 3#>
804- default:
805- <#otherwise, do something else#>
799+ case <#value 1#>:
800+ <#respond to value 1#>
801+ case <#value 2#>,
802+ <#value 3#>:
803+ <#respond to value 2 or 3#>
804+ default:
805+ <#otherwise, do something else#>
806806}
807807```
808808
@@ -832,12 +832,12 @@ a single lowercase character called `someCharacter`:
832832``` swift
833833let someCharacter: Character = " z"
834834switch someCharacter {
835- case " a" :
836- print (" The first letter of the alphabet" )
837- case " z" :
838- print (" The last letter of the alphabet" )
839- default :
840- print (" Some other character" )
835+ case " a" :
836+ print (" The first letter of the alphabet" )
837+ case " z" :
838+ print (" The last letter of the alphabet" )
839+ default :
840+ print (" Some other character" )
841841}
842842// Prints "The last letter of the alphabet"
843843```
@@ -891,11 +891,11 @@ It isn't valid to write the following code, because the first case is empty:
891891``` swift
892892let anotherCharacter: Character = " a"
893893switch anotherCharacter {
894- case " a" : // Invalid, the case has an empty body
895- case " A" :
896- print (" The letter A" )
897- default :
898- print (" Not the letter A" )
894+ case " a" : // Invalid, the case has an empty body
895+ case " A" :
896+ print (" The letter A" )
897+ default :
898+ print (" Not the letter A" )
899899}
900900// This will report a compile-time error.
901901```
@@ -936,10 +936,10 @@ separating the values with commas.
936936``` swift
937937let anotherCharacter: Character = " a"
938938switch anotherCharacter {
939- case " a" , " A" :
940- print (" The letter A" )
941- default :
942- print (" Not the letter A" )
939+ case " a" , " A" :
940+ print (" The letter A" )
941+ default :
942+ print (" Not the letter A" )
943943}
944944// Prints "The letter A"
945945```
@@ -1050,16 +1050,16 @@ and categorizes it on the graph that follows the example.
10501050``` swift
10511051let somePoint = (1 , 1 )
10521052switch somePoint {
1053- case (0 , 0 ):
1054- print (" \( somePoint ) is at the origin" )
1055- case (_ , 0 ):
1056- print (" \( somePoint ) is on the x-axis" )
1057- case (0 , _ ):
1058- print (" \( somePoint ) is on the y-axis" )
1059- case (-2 ... 2 , -2 ... 2 ):
1060- print (" \( somePoint ) is inside the box" )
1061- default :
1062- print (" \( somePoint ) is outside of the box" )
1053+ case (0 , 0 ):
1054+ print (" \( somePoint ) is at the origin" )
1055+ case (_ , 0 ):
1056+ print (" \( somePoint ) is on the x-axis" )
1057+ case (0 , _ ):
1058+ print (" \( somePoint ) is on the y-axis" )
1059+ case (-2 ... 2 , -2 ... 2 ):
1060+ print (" \( somePoint ) is inside the box" )
1061+ default :
1062+ print (" \( somePoint ) is outside of the box" )
10631063}
10641064// Prints "(1, 1) is inside the box"
10651065```
@@ -1117,12 +1117,12 @@ and categorizes it on the graph that follows:
11171117``` swift
11181118let anotherPoint = (2 , 0 )
11191119switch anotherPoint {
1120- case (let x, 0 ):
1121- print (" on the x-axis with an x value of \( x ) " )
1122- case (0 , let y):
1123- print (" on the y-axis with a y value of \( y ) " )
1124- case let (x, y):
1125- print (" somewhere else at (\( x ) , \( y ) )" )
1120+ case (let x, 0 ):
1121+ print (" on the x-axis with an x value of \( x ) " )
1122+ case (0 , let y):
1123+ print (" on the y-axis with a y value of \( y ) " )
1124+ case let (x, y):
1125+ print (" somewhere else at (\( x ) , \( y ) )" )
11261126}
11271127// Prints "on the x-axis with an x value of 2"
11281128```
@@ -1182,12 +1182,12 @@ The example below categorizes an (x, y) point on the following graph:
11821182``` swift
11831183let yetAnotherPoint = (1 , -1 )
11841184switch yetAnotherPoint {
1185- case let (x, y) where x == y:
1186- print (" (\( x ) , \( y ) ) is on the line x == y" )
1187- case let (x, y) where x == - y:
1188- print (" (\( x ) , \( y ) ) is on the line x == -y" )
1189- case let (x, y):
1190- print (" (\( x ) , \( y ) ) is just some arbitrary point" )
1185+ case let (x, y) where x == y:
1186+ print (" (\( x ) , \( y ) ) is on the line x == y" )
1187+ case let (x, y) where x == - y:
1188+ print (" (\( x ) , \( y ) ) is on the line x == -y" )
1189+ case let (x, y):
1190+ print (" (\( x ) , \( y ) ) is just some arbitrary point" )
11911191}
11921192// Prints "(1, -1) is on the line x == -y"
11931193```
@@ -1240,13 +1240,13 @@ For example:
12401240``` swift
12411241let someCharacter: Character = " e"
12421242switch someCharacter {
1243- case " a" , " e" , " i" , " o" , " u" :
1244- print (" \( someCharacter ) is a vowel" )
1245- case " b" , " c" , " d" , " f" , " g" , " h" , " j" , " k" , " l" , " m" ,
1246- " n" , " p" , " q" , " r" , " s" , " t" , " v" , " w" , " x" , " y" , " z" :
1247- print (" \( someCharacter ) is a consonant" )
1248- default :
1249- print (" \( someCharacter ) isn't a vowel or a consonant" )
1243+ case " a" , " e" , " i" , " o" , " u" :
1244+ print (" \( someCharacter ) is a vowel" )
1245+ case " b" , " c" , " d" , " f" , " g" , " h" , " j" , " k" , " l" , " m" ,
1246+ " n" , " p" , " q" , " r" , " s" , " t" , " v" , " w" , " x" , " y" , " z" :
1247+ print (" \( someCharacter ) is a consonant" )
1248+ default :
1249+ print (" \( someCharacter ) isn't a vowel or a consonant" )
12501250}
12511251// Prints "e is a vowel"
12521252```
@@ -1289,10 +1289,10 @@ and that the value always has the same type.
12891289``` swift
12901290let stillAnotherPoint = (9 , 0 )
12911291switch stillAnotherPoint {
1292- case (let distance, 0 ), (0 , let distance):
1293- print (" On an axis, \( distance ) from the origin" )
1294- default :
1295- print (" Not on an axis" )
1292+ case (let distance, 0 ), (0 , let distance):
1293+ print (" On an axis, \( distance ) from the origin" )
1294+ default :
1295+ print (" Not on an axis" )
12961296}
12971297// Prints "On an axis, 9 from the origin"
12981298```
@@ -1431,16 +1431,16 @@ For brevity, multiple values are covered in a single `switch` case.
14311431let numberSymbol: Character = " 三" // Chinese symbol for the number 3
14321432var possibleIntegerValue: Int ?
14331433switch numberSymbol {
1434- case " 1" , " ١" , " 一" , " ๑" :
1435- possibleIntegerValue = 1
1436- case " 2" , " ٢" , " 二" , " ๒" :
1437- possibleIntegerValue = 2
1438- case " 3" , " ٣" , " 三" , " ๓" :
1439- possibleIntegerValue = 3
1440- case " 4" , " ٤" , " 四" , " ๔" :
1441- possibleIntegerValue = 4
1442- default :
1443- break
1434+ case " 1" , " ١" , " 一" , " ๑" :
1435+ possibleIntegerValue = 1
1436+ case " 2" , " ٢" , " 二" , " ๒" :
1437+ possibleIntegerValue = 2
1438+ case " 3" , " ٣" , " 三" , " ๓" :
1439+ possibleIntegerValue = 3
1440+ case " 4" , " ٤" , " 四" , " ๔" :
1441+ possibleIntegerValue = 4
1442+ default :
1443+ break
14441444}
14451445if let integerValue = possibleIntegerValue {
14461446 print (" The integer value of \( numberSymbol ) is \( integerValue ) ." )
@@ -1520,11 +1520,11 @@ The example below uses `fallthrough` to create a textual description of a number
15201520let integerToDescribe = 5
15211521var description = " The number \( integerToDescribe ) is"
15221522switch integerToDescribe {
1523- case 2 , 3 , 5 , 7 , 11 , 13 , 17 , 19 :
1524- description += " a prime number, and also"
1525- fallthrough
1526- default :
1527- description += " an integer."
1523+ case 2 , 3 , 5 , 7 , 11 , 13 , 17 , 19 :
1524+ description += " a prime number, and also"
1525+ fallthrough
1526+ default :
1527+ description += " an integer."
15281528}
15291529print (description)
15301530// Prints "The number 5 is a prime number, and also an integer."
@@ -1664,16 +1664,16 @@ gameLoop: while square != finalSquare {
16641664 diceRoll += 1
16651665 if diceRoll == 7 { diceRoll = 1 }
16661666 switch square + diceRoll {
1667- case finalSquare:
1668- // diceRoll will move us to the final square, so the game is over
1669- break gameLoop
1670- case let newSquare where newSquare > finalSquare:
1671- // diceRoll will move us beyond the final square, so roll again
1672- continue gameLoop
1673- default :
1674- // this is a valid move, so find out its effect
1675- square += diceRoll
1676- square += board[square]
1667+ case finalSquare:
1668+ // diceRoll will move us to the final square, so the game is over
1669+ break gameLoop
1670+ case let newSquare where newSquare > finalSquare:
1671+ // diceRoll will move us beyond the final square, so roll again
1672+ continue gameLoop
1673+ default :
1674+ // this is a valid move, so find out its effect
1675+ square += diceRoll
1676+ square += board[square]
16771677 }
16781678}
16791679print (" Game over!" )
0 commit comments