Skip to content

Commit af4c97c

Browse files
authored
Add docs about iterating a for loop using zip (#38314)
1 parent 349e072 commit af4c97c

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

doc/src/manual/control-flow.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -553,6 +553,21 @@ julia> for i = 1:2, j = 3:4
553553
If this example were rewritten to use a `for` keyword for each variable, then the output would
554554
be different: the second and fourth values would contain `0`.
555555

556+
Multiple containers can be iterated over at the same time in a single `for` loop using [`zip`](@ref):
557+
558+
```jldoctest
559+
julia> for (j, k) in zip([1 2 3], [4 5 6 7])
560+
println((j,k))
561+
end
562+
(1, 4)
563+
(2, 5)
564+
(3, 6)
565+
```
566+
567+
Using [`zip`](@ref) will create an iterator that is a tuple containing the subiterators for the containers passed to it.
568+
The `zip` iterator will iterate over all subiterators in order, choosing the ``i``th element of each subiterator in the
569+
``i``th iteration of the `for` loop. Once any of the subiterators run out, the `for` loop will stop.
570+
556571
## Exception Handling
557572

558573
When an unexpected condition occurs, a function may be unable to return a reasonable value to

0 commit comments

Comments
 (0)