Skip to content

Commit 6f5b73e

Browse files
Show examples that actually need 'case'
Co-authored-by: Doug Gregor <dgregor@apple.com>
1 parent 2d80047 commit 6f5b73e

File tree

1 file changed

+34
-15
lines changed

1 file changed

+34
-15
lines changed

TSPL.docc/LanguageGuide/ControlFlow.md

Lines changed: 34 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1459,18 +1459,19 @@ In this code,
14591459
the condition for the `if` statement starts with `case`,
14601460
indicating that the condition is a pattern instead of a Boolean value.
14611461
If the pattern matches,
1462-
the condition for the `if` is considered to be true,
1463-
and the code in the body of the `if` statement runs.
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.
14641464
The patterns you can write after `if case`
14651465
are the same as the patterns you can write in a switch case.
14661466

1467-
You can also use a pattern in a `for`-`in` loop,
1468-
to give names to parts of the value using a value binding:
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:
14691470

14701471
```swift
1471-
let points = [(10, 0), (30, 100), (-20, 0)]
1472+
let points = [(10, 0), (30, -30), (-20, 0)]
14721473

1473-
for case let (x, y) in points {
1474+
for (x, y) in points {
14741475
if y == 0 {
14751476
print("Found a point on the x-axis at \(x)")
14761477
}
@@ -1484,29 +1485,47 @@ binding the first and second elements of the tuples
14841485
to the `x` and `y` constants.
14851486
The statements inside the loop can use those constants,
14861487
like the `if` statement that checks whether the point lies on the x-axis.
1487-
1488-
A `for`-`case`-`in` loop can also include a `where` clause,
1489-
to check for an additional condition.
1490-
The statements inside the loop run
1491-
only when `where` clause matches the current element.
1492-
For example,
1493-
the `for`-`case`-`in` loop below is the same as the `for`-`in` loop above.
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:
14941492

14951493
```swift
1496-
for case let (x, y) in points where y == 0 {
1494+
for case (let x, 0) in points {
14971495
print("Found a point on the x-axis at \(x)")
14981496
}
14991497
// Prints "Found a point on the x-axis at 10"
15001498
// Prints "Found a point on the x-axis at -20"
15011499
```
15021500

15031501
In this code,
1504-
the condition is integrated into the `for`-`case`-`in` loop as part of the pattern.
1502+
the condition is integrated into the `for`-`case`-`in` loop
1503+
as part of the pattern.
15051504
The statements in the `for`-`case`-`in` loop run only for points on the x-axis.
15061505
This code produces the same result as the `for`-`in` loop above,
15071506
but is a more compact way to iterate
15081507
over only certain elements in a collection.
15091508

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+
15101529
Because patterns can bind values,
15111530
`if`-`case` statements and `for`-`case`-`in` loops
15121531
are useful for working with enumerations that have associated values,

0 commit comments

Comments
 (0)