for loop in
, ∈
, =
Below three for loops return the same output. Note that in
, ∈
, =
can be used interchangeably to indicate that i
loops over the following array, 1:2:10
which indicates that the array ranges from 1 to 10 with an increment of 2.
for i in 1:2:10
println(i)
end
for i ∈ 1:2:10
println(i)
end
for i = 1:2:10
println(i)
end
Output:
1
3
5
7
9
eachindex
I also introduce in this post eachindex
, which returns the index of a given vector. Note that the array 1:2:10
has five elements, and i
will loop over the five indices with eachindex(1:2:10)
.
for i = eachindex(1:2:10)
println(i)
end
Output:
1
2
3
4
5
Reference
https://julia.quantecon.org/getting_started_julia/julia_by_example.html