@@ -1440,6 +1440,97 @@ and `distance` is an integer in both patterns ---
14401440which means that the code in the body of the ` case `
14411441can always access a value for ` distance ` .
14421442
1443+ ## Patterns
1444+
1445+ In the previous examples, each switch case includes a pattern
1446+ that indicates what values match that case.
1447+ You can also use a pattern as the condition for an ` if ` statement.
1448+ Here's what that looks like:
1449+
1450+ ``` swift
1451+ let somePoint = (12 , 100 )
1452+ if case (let x, 100 ) = somePoint {
1453+ print (" Found a point on the y=100 line, at \( x ) " )
1454+ }
1455+ // Prints "Found a point on the y=100 line, at 12"
1456+ ```
1457+
1458+ In this code,
1459+ the condition for the ` if ` statement starts with ` case ` ,
1460+ indicating that the condition is a pattern instead of a Boolean value.
1461+ If the pattern matches,
1462+ then the condition for the ` if ` is considered to be true,
1463+ and so the code in the body of the ` if ` statement runs.
1464+ The patterns you can write after ` if case `
1465+ are the same as the patterns you can write in a switch case.
1466+
1467+ In a ` for ` -` in ` loop,
1468+ you can give names to parts of the value using a value binding pattern,
1469+ even without writing ` case ` in your code:
1470+
1471+ ``` swift
1472+ let points = [(10 , 0 ), (30 , -30 ), (-20 , 0 )]
1473+
1474+ for (x, y) in points {
1475+ if y == 0 {
1476+ print (" Found a point on the x-axis at \( x ) " )
1477+ }
1478+ }
1479+ // Prints "Found a point on the x-axis at 10"
1480+ // Prints "Found a point on the x-axis at -20"
1481+ ```
1482+
1483+ The ` for ` -` in ` loop above iterates over an array of tuples,
1484+ binding the first and second elements of the tuples
1485+ to the ` x ` and ` y ` constants.
1486+ The statements inside the loop can use those constants,
1487+ such as the ` if ` statement that checks whether the point lies on the x-axis.
1488+ A more concise way to write this code
1489+ is to combine the value bindings and condition
1490+ using a ` for ` -` case ` -` in ` loop.
1491+ The code below has the same behavior as the ` for ` -` in ` loop above:
1492+
1493+ ``` swift
1494+ for case (let x, 0 ) in points {
1495+ print (" Found a point on the x-axis at \( x ) " )
1496+ }
1497+ // Prints "Found a point on the x-axis at 10"
1498+ // Prints "Found a point on the x-axis at -20"
1499+ ```
1500+
1501+ In this code,
1502+ the condition is integrated into the ` for ` -` case ` -` in ` loop
1503+ as part of the pattern.
1504+ The statements in the ` for ` -` case ` -` in ` loop run only for points on the x-axis.
1505+ This code produces the same result as the ` for ` -` in ` loop above,
1506+ but is a more compact way to iterate
1507+ over only certain elements in a collection.
1508+
1509+ A ` for ` -` case ` -` in ` loop can also include a ` where ` clause,
1510+ to check for an additional condition.
1511+ The statements inside the loop run
1512+ only when ` where ` clause matches the current element.
1513+ For example:
1514+
1515+ ``` swift
1516+ for case let (x, y) in points where x == y || x == - y {
1517+ print (" Found (\( x ) , \( y ) ) along a line through the origin" )
1518+ }
1519+ // Prints "Found (30, -30) along a line through the origin"
1520+ ```
1521+
1522+ This code binds the first and second elements of the tuple
1523+ to ` x ` and ` y ` as constants,
1524+ and then checks their values in the ` where ` clause.
1525+ If the ` where ` clause is ` true ` ,
1526+ then the code in the body of the ` for ` loop runs;
1527+ otherwise, iteration continues with the next element.
1528+
1529+ Because patterns can bind values,
1530+ ` if ` -` case ` statements and ` for ` -` case ` -` in ` loops
1531+ are useful for working with enumerations that have associated values,
1532+ as described in < doc:Enumerations#Associated-Values > .
1533+
14431534## Control Transfer Statements
14441535
14451536* Control transfer statements* change the order in which your code is executed,
0 commit comments