yH[[[y{H{h[{h[{h[y{H[[[y{HHf{fpfV[:f[:f[:f[:[:::y{PHf[f[f[{[[/{9jj{jHfEE[yEy[{{yyy{hH[[[hy{jpHh[y{9hH[h[h[hy{9HHf[f[y{HHHMy{sHHMf[^wlFbbrFBAEErEXW~| 755455V G75656565VC G75545VCC AG5977V AG746VC 5G77866V G75458VC G754VC G754545V G7F- G7F- G7F-

Switch.` A variable has a value. In a switch statement, we test for all possible values. A switch must have a case for all possibilities.`Keywords.` With the case keyword, we match a value or an interval. A let statement can introduce a temporary constant. With "where," we add checks.`A simple example.` This program uses a simple switch statement. It assigns the constant "id" to 10. It then switches on that value. `Result: `The "case 10" is reached and the program displays "Ten." The switch has a case for all possible values.`Default: `This will match all values other than 9, 10 and 11 in the switch. In Swift, a case must match all possibilities.`Multiple case values.` Sometimes a case should match multiple values. We cannot "stack" cases. Instead we must specify all possible values in one case. `Ranges.` A range is specified with a start, end and three periods in between. Ranges can be used in switch statements. All included values are matched by the range case. `The "..<" operator, which is a range that goes until one less than the max, is also used in switches.`Fallthrough.` This keyword means that control proceeds to the next case in a switch. The next case is entered (even if the value does not match). `This behavior is the default in some C-like languages. But in Swift it is specified with a special keyword.`Case 2 is entered, but then case 1 and case 0 are entered because of the "fallthrough" statements.`String.` In Swift we can use a string as the value being switched upon. We can use literals (like "cat") to match our string. The contents of the strings are compared. `Where.` Sometimes we want further checking in a case. For example, we might want to check two variables (both parts of a tuple) at once. We use "where" for this. `We call the test() method with tuple arguments. When the first item of a tuple is greater than or equal to 10, we print our values.`Tuple.` Here we switch on a tuple. We use a "tuple pattern" to match all items in the tuple. Many syntax features, like "let" and underscores, can be used to match parts of tuples. `Let values.` A case block can capture values in a tuple switch. We use the "let" keyword and provide an identifier (like "size" in this example). `Result: `The first item in the tuple matches the value "monkey" and the size value is set to 200 from the tuple being switched upon.`Tuples, any value.` With an underscore, we can match just parts of a tuple in a switch. We specify the underscore to "ignore" items in a tuple case. `We match only the second element in a 3-item tuple. The first and third items are ignored.`Exhaustive, error.` A switch cannot just match some cases. It must match all possible cases. The default block is useful for handling rare or unusual (unexpected) cases. `At least one case, error.` A switch cannot be empty. It must have at least one case or a default block. The Swift compiler will report an error on an empty switch. `Tuple pattern, error.` For tuple patterns in a switch, each case must have the correct count of items. We cannot match a 2-item tuple with a 3-item tuple pattern case. `A review.` Switch is a powerful construct in Swift. It has unique features in this language. They are designed to make flow easier to design and control.

VXY let idX10 YXY id { YcaseY 9: VX("Nine") YcaseY 10: VX(Y"Ten"Y) YcaseY 11: VX("Eleven") YdefaultY: Vbreak } Y TenY let sizeX3Y XSwitch on the size Int. YXY size { YcaseY 0, 1:Y VXTwo Xs match Small. VYX("Small") YcaseY 2, 3:Y VXTwo Xs match Medium. VYX("Medium") YcaseY 4, 5:Y VXTwo Xs match Large. VYX("Large") YdefaultY: Vbreak } Y MediumY let codeX70Y XSwitch based on ranges. YXY code { YcaseY 0...50: VX("Lower half") YcaseY 51...100: VX(Y"Upper half"Y) YdefaultY: VX("Invalid") } Y Upper halfY let sizeXY2Y XUse X with fallthrough statements. YXY size { X3:Y VXThis Xwill execute statementsXX2, 1X0. VYX("Number cX 3") VYfallthroughY X2: VX("Number cX 2") VYfallthroughY X1: VX("Number cX 1") VYfallthroughY X0: VX("Number cX 0") default: VX("Invalid") } Y Number cX 2 Number cX 1 Number cX 0Y let nameXY"cat"Y XSwitch on the name X. YXY name { X"bird": VX("Is bird") X"dog": VX("Is dog") X"cat": VX(Y"Is cat"Y)Y XThis is Xed. Ydefault: VX("Something else") } Y Is catY YfuncY test(code: (Int, CX)) {Y VXSwitch on the tuple argument. VXWe use letXallow the tuple itemsXbe referenced. VXVWe use whereXtest a part of the tuple. VYXY code { VXlet(X, letter) YwhereY X >= 10: VVX(Y"NumberX\(X), LetterX\(letter)"Y) Vdefault: VVX(Y"Default"Y) V} }Y XCall test with a tuple argument. Ytest(code: (5, "a"))Y XCall test again. Ytest(code: (15, "b")) Y Default NumberX15, LetterXbY let dataX("xyz", 200)Y XMatch complete tuple Xs. YXY (data) { YcaseY ("abc", 300): X(Y"Is abc300"Y) YcaseY ("xyz", 200): X(Y"Is xyz200"Y) default: X(Y"Not known"Y) } Y Is xyz200Y let XX("monkey", 200)Y XUse letXcapture a variableXa tuple. YXY (X) { YcaseY ("monkey", let size): X(Y"Monkey has size \(size)"Y) default: break } Y Monkey has size 200Y let tupleX("cat", 1, "penguin")Y XSwitch on tuple. XMatch second X of the tuple. YXY (tuple) { YcaseY (_, 1, _): X(Y"Second X is 1"Y) YcaseY (_, 2, _): X(Y"Second X is 2"Y) YdefaultY: X("No case") } Y Second X is 1Y let heightX70Y XThis program will not work. XA X must have a default clause. YXY (height) { X60: X(true) X70: X(true) } YResultsY /main.swift:8:1: Switch must be exhaustive, consider adding a default clauseY let xX5Y XA X cannot be empty. YXY (x) { } YResultsY /main.swift:5:1: 'X' statement body must have at least one 'case' or 'default' blockY let XX(10, 20)Y XXa tuple X, the pattern must be correct. YXY (X) { X(10, 20, 30): X("OK") } YResultsY /main.swift:3:6: Tuple pattern has the wrong lX Xtuple type '(Int, Int)'Y

,Vd_f`d\aA39:switchmultiple case valuesranges in casesfallthrough statementsswitch on stringwhere in switch casetuple switchlet, value switchswitches, matches any tuple valuecauses switch errorcauses empty switch errorcauses tuple pattern error