for loops

We already encountered the for loop when iterating over the element e of a collection coll (refer to the StringsRanges and Arrays sections in Chapter 2, Variables, Types, and Operations). This takes the following 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). The variable e is not known outside the for loop. When iterating over a numeric range, often = (equal to) is used instead of in:

    for n = 1:10   
       print(n^3)   
    end 

(This code can be a one-liner, but is spread over three lines for clarity.) The for loop is generally ...

Get Julia 1.0 Programming now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.