Repeated evaluation
Julia has a for
loop for iterating over a collection or repeating some code a certain number of times. You can use a while
loop when the repetition depends on a condition and you can influence the execution of both loops through break
and continue
.
The for loop
We already encountered the
for
loop when iterating over the elements e
of a collection coll
(refer to the Strings and Ranges and Arrays sections in
Chapter 2, Variables, Types, and Operations). This takes the general form:
# code in Chapter 4\repetitions.jl
for e in coll
# body: process(e) executed for every element e in coll
end
Here, coll
can be a range, a string, an array, or any other iterable collection (for other uses, also refer to Chapter 5, Collection Types). ...
Get Getting Started with Julia now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.